Mitchell931993

Untitled

Apr 14th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Validation for signup page
  2. authors:Mitchell Klein n8281866
  3.         Luke Meggison nXXXXXXXX
  4. */
  5. // ADD IN NUMBER DETECTION FOR LOOP
  6. function testEmpty(fieldName,value,mutipleFields){//Tests fields that just need data
  7.     multipleFields = (multipleFields)? "s are" : " is";
  8.     if (value==null || value==""){     
  9.         output = output.concat("- The ", fieldname, " field", mutipleFields, " empty\n");
  10.         return false;//Field is empty
  11.     }else {
  12.         return true;//field is not empty
  13.     }
  14. }//End Function
  15.  
  16. function testTextOnly(fieldname, value){
  17.     var alphabet = /[a-zA-z]/;//Generates array will all lowercase and upper case characters
  18.     if (alphabet.test(value)){//This does not allow users to have numbers in this field
  19.         return true;
  20.     } else {
  21.         output = output.concat("- The", fieldname, " field should be letters only.\n");
  22.         return false;
  23.     }
  24.    
  25. }//End Function
  26.  
  27. function testPhoneNumber(number){
  28.     if (!isNaN(parseFloat(number))){
  29.         return true;
  30.     } else {
  31.         error=true;
  32.         output = output.concat( "- The Phone Number is not valid.\n");
  33.         retun false;
  34.     }
  35. }
  36.  
  37. function testEmail(field){ //tests if contains an @ and "." in the correct position
  38.     var atPosition= field.indexOf("@");
  39.     var dotPosition= field.lastIndexOf(".");
  40.     if (atPosition<1 || dotposition+2>=field.length || dotPosition<atPosition+2){
  41.         error= true;
  42.         output = output.concat("- Entered Email Addresses must be real.\n");
  43.         return false;
  44.     } else{
  45.         return true;
  46.     }// end else If (symbol test)
  47. }
  48.  
  49. function testValidPassword(value){
  50.     var number = /[0-9]/;
  51.     var alphabet = /[a-zA-z]/;
  52.     var containsNumber = new boolean(false);
  53.     var containsAlpha = new boolean(false);
  54.     for (x=0; x<=value.length; x++){
  55.         if (alphabet.test(value[x])){
  56.             containsAlpha = true;
  57.         }else if(number.test(value[x])){
  58.             containsNumber = true;
  59.         }else {
  60.             return false; //is not a valid password, halts function and returns false
  61.         }//End if, elseif, else    
  62.     }//End for loop
  63.     if ((value.length>=8)&& containsNumber && containsAlpha){
  64.         return true;
  65.     } else{
  66.         output = output.concat("- Passwords must be atleast 8 alphanumeric\n  characters long and have atleast one number\n");
  67.         return false;
  68.     }//End retuns
  69. }//End function
  70.  
  71. function validation(this){//designed to be universal and work with any form
  72.     var element = this.elements.length;//used as a reference for all elements inside of the form
  73.     var error = new boolean(false);
  74.     var output = new string("The following errors have occured:\n");
  75.    
  76.     for (x=0; x<=elements; x++){
  77.         //name is assigned by developer and is set to a predefined "email, firstname, last name etc"
  78.         var name=this.elements[x].name;
  79.         var value=this.elements[x].value; //Entry withint text boxes etc
  80.        
  81.         switch(name){
  82.             case "firstName":
  83.                 if ((!testEmpty("First Name", value))||(!testTextOnly("'First Name'", value))){
  84.                     error = true;
  85.                 }
  86.                 break;
  87.             case "lastName":
  88.                 if ((!testEmpty("Last Name",value))||(!testTextOnly("'Last Name'", value))){
  89.                     error = true;
  90.                 }
  91.                 break;
  92.             case "emailAddress":
  93.                 var confirmEmailAddress = this.getElemtentsByName("confirmEmailAddress").value;
  94.                 if ((!testEmpty("Email Address's",value))||(!string.compare(value, confirmEmailAddress))){
  95.                     error = true;
  96.                     output = output.concat("-Email fields do not match");
  97.                 }
  98.                 else if (!testEmail(value)){
  99.                     error = true;
  100.                 }
  101.                 break;
  102.             /*No need to include a confirm email case because a mismatch takes priotiry over
  103.             whether the email address is valid. ANd there is no need to test if the confirm
  104.             email address is valid if the first is valid and the two match */
  105.             case "phoneNumber":
  106.                 if ((!empty("Phone Number",value))||(!testPhoneNumber(value))){
  107.                     error=true;
  108.                     }
  109.                 break;
  110.             case "password":
  111.                 var confirmPassword = new string(this.getElementsByName("confirmPassword").value);
  112.                 if ((!testEmpty("Password ",value))|| (!string.compare(value, confirmPassword))){
  113.                     x++;//skips the next element - if set up correctly it will be the confirm field
  114.                     error = true;
  115.                     output = output.concat( "- Passwords do not match.\n");
  116.                 }else if (!testValidPassword(value)){
  117.                     error = true;
  118.                 }
  119.                 break;
  120.             //Testing has already been done, but they still need to be included in the "predefined list"
  121.             case "confirmEmailAddress":
  122.             case "confirmPassword":
  123.                 break;
  124.             default:
  125.                 alert("There was a field that was not on the predefined list");
  126.                 break;
  127.         }//End switch Statement
  128.     }//End for loop
  129.     alert(output);
  130.     if (error){
  131.         return false;//halts form and requires user to fix data
  132.        
  133.     } else {
  134.         return true;//continues to next page
  135.     }
  136. }//End Validation function
Advertisement
Add Comment
Please, Sign In to add comment