Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Date Completed: July 18, 2017
- // Purpose: Validating a Canadian Postal Code
- // This line makes the button, btnValidate wait for a mouse click
- // When the button is clicked, the determineValidity function is called
- btnValidate.addEventListener(MouseEvent.CLICK, determineValidity);
- // This line makes the textinputs: txtinOrigin wait for a mouse click
- // When a textinput is clicked, the clearLabel 1 functions are called
- txtinOrigin.addEventListener(MouseEvent.CLICK, clearLabel1);
- // This is the determineValidity function
- // e:MouseEvent is the click event experienced by the button
- // void indicates that the function does not return a value
- function determineValidity(e:MouseEvent):void
- {
- // Declare the variables
- var Origin:String;
- //Declare the value of the Origin
- Origin = String(txtinOrigin.text);
- //Get Unicode values for capital A, Z, and assign the value to Space
- var Values:String = "AZ 09";
- var Value1:Number = Values.charCodeAt(0);
- var Value2:Number = Values.charCodeAt(1);
- var Value3:Number = Values.charCodeAt(2);
- var Value4:Number = Values.charCodeAt(3);
- var Value5:Number = Values.charCodeAt(4);
- // Determining validity of postal code
- // If the characters do not match to the Unicode value and they are invalid
- if (Origin.charCodeAt(0) < Value1 || Origin.charCodeAt(0) > Value2)
- {
- lblResult.text = "Not a valid postal code." + "\r" +"The first character is incorrect.";
- }
- else if (Origin.length < 4)
- {
- lblResult.text = "Insufficient characters" + "\r" + "A postal code consists of six alphanumeric characters and a space between the first and last three characters." + "\r" + "e.g. H0H 0H0";
- }
- else if (Origin.charCodeAt(1) < Value4 || Origin.charCodeAt (1) > Value5)
- {
- lblResult.text = "Not a valid postal code." + "\r" + "The second character is incorrect.";
- }
- else if (Origin.charCodeAt(2) < Value1 || Origin.charCodeAt(2) > Value2)
- {
- lblResult.text = "Not a valid postal code." + "\r" + "The third character is incorrect.";
- }
- else if (Origin.charCodeAt(3) != Value3 )
- {
- lblResult.text = "Not a valid postal code." + "\r" + "The fourth character is incorrect." + "\r" + "There must be a space between the first three and last three characters of your postal code. ";
- }
- else if (Origin.charCodeAt(4) < Value4 || Origin.charCodeAt (4) > Value5)
- {
- lblResult.text = "Not a valid postal code." + "\r" + "The fifth character is incorrec.t";
- }
- else if (Origin.charCodeAt(5) < Value1 || Origin.charCodeAt(5) > Value2)
- {
- lblResult.text = "Not a valid postal code." + "\r"+ "The sixth character is incorrect.";
- }
- else if (Origin.charCodeAt(6) < Value4 || Origin.charCodeAt (6) > Value5)
- {
- lblResult.text = "Not a valid postal code." + "\r" + "The seventh character is incorrect.";
- }
- else if (Origin.length < 7)
- {
- lblResult.text = "Insufficient characters" + "\r" + "A postal code consists of six alphanumeric characters and a space between the first and last three characters." + "\r" + "e.g. H0H 0H0";
- }
- else
- {
- lblResult.text = "This is a valid postal code.";
- }
- }
- // This is the clearLabel1 function
- // e:MouseEvent is the click event experienced by the textinput
- // void indicates that the function does not return a value
- function clearLabel1(e:MouseEvent):void
- {
- lblResult.text = "";
- txtinOrigin.text = "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement