Advertisement
BOT_Yokel

ICS3U1 Unit 2 Activity 1: Question 4

Jul 18th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Date Completed: July 18, 2017
  2. // Purpose: Validating a Canadian Postal Code
  3.  
  4. // This line makes the button, btnValidate wait for a mouse click
  5. // When the button is clicked, the determineValidity function is called
  6. btnValidate.addEventListener(MouseEvent.CLICK, determineValidity);
  7.  
  8. // This line makes the textinputs: txtinOrigin wait for a mouse click
  9. // When a textinput is clicked, the clearLabel 1 functions are called
  10. txtinOrigin.addEventListener(MouseEvent.CLICK, clearLabel1);
  11.  
  12. // This is the determineValidity function
  13. // e:MouseEvent is the click event experienced by the button
  14. // void indicates that the function does not return a value
  15. function determineValidity(e:MouseEvent):void
  16. {
  17.  
  18.     // Declare the variables
  19.     var Origin:String;
  20.  
  21.     //Declare the value of the Origin
  22.     Origin = String(txtinOrigin.text);
  23.    
  24.     //Get Unicode values for capital A, Z, and assign the value to Space
  25.     var Values:String = "AZ 09";
  26.     var Value1:Number = Values.charCodeAt(0);
  27.     var Value2:Number = Values.charCodeAt(1);
  28.     var Value3:Number = Values.charCodeAt(2);
  29.     var Value4:Number = Values.charCodeAt(3);
  30.     var Value5:Number = Values.charCodeAt(4);
  31.  
  32.     // Determining validity of postal code
  33.     // If the characters do not match to the Unicode value and they are invalid
  34.     if (Origin.charCodeAt(0) < Value1 || Origin.charCodeAt(0) > Value2)
  35.     {
  36.         lblResult.text = "Not a valid postal code." + "\r" +"The first character is incorrect.";
  37.     }
  38.    
  39.     else if (Origin.length < 4)
  40.     {
  41.         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";
  42.     }
  43.    
  44.     else if (Origin.charCodeAt(1) < Value4 || Origin.charCodeAt (1) > Value5)
  45.     {
  46.         lblResult.text = "Not a valid postal code." + "\r" + "The second character is incorrect.";
  47.     }
  48.    
  49.     else if (Origin.charCodeAt(2) < Value1 || Origin.charCodeAt(2) > Value2)
  50.     {
  51.         lblResult.text = "Not a valid postal code." + "\r" + "The third character is incorrect.";
  52.     }
  53.    
  54.     else if (Origin.charCodeAt(3) != Value3 )
  55.     {
  56.         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. ";
  57.     }
  58.    
  59.     else if (Origin.charCodeAt(4) < Value4 || Origin.charCodeAt (4) > Value5)
  60.     {
  61.         lblResult.text = "Not a valid postal code." + "\r" + "The fifth character is incorrec.t";
  62.     }
  63.    
  64.     else if (Origin.charCodeAt(5) < Value1 || Origin.charCodeAt(5) > Value2)
  65.     {
  66.         lblResult.text = "Not a valid postal code." + "\r"+ "The sixth character is incorrect.";
  67.     }
  68.    
  69.     else if (Origin.charCodeAt(6) < Value4 || Origin.charCodeAt (6) > Value5)
  70.     {
  71.         lblResult.text = "Not a valid postal code." + "\r" + "The seventh character is incorrect.";
  72.     }
  73.    
  74.     else if (Origin.length < 7)
  75.     {
  76.         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";
  77.     }
  78.    
  79.     else
  80.     {
  81.         lblResult.text = "This is a valid postal code.";
  82.     }
  83.  
  84.  }
  85.  
  86. // This is the clearLabel1 function
  87. // e:MouseEvent is the click event experienced by the textinput
  88. // void indicates that the function does not return a value
  89. function clearLabel1(e:MouseEvent):void
  90.  
  91. {
  92.     lblResult.text = "";
  93.     txtinOrigin.text = "";
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement