Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Validation for signup page
- authors:Mitchell Klein n8281866
- Luke Meggison nXXXXXXXX
- */
- // ADD IN NUMBER DETECTION FOR LOOP
- function testEmpty(fieldName,value,mutipleFields){//Tests fields that just need data
- multipleFields = (multipleFields)? "s are" : " is";
- if (value==null || value==""){
- output = output.concat("- The ", fieldname, " field", mutipleFields, " empty\n");
- return false;//Field is empty
- }else {
- return true;//field is not empty
- }
- }//End Function
- function testTextOnly(fieldname, value){
- var alphabet = /[a-zA-z]/;//Generates array will all lowercase and upper case characters
- if (alphabet.test(value)){//This does not allow users to have numbers in this field
- return true;
- } else {
- output = output.concat("- The", fieldname, " field should be letters only.\n");
- return false;
- }
- }//End Function
- function testPhoneNumber(number){
- if (!isNaN(parseFloat(number))){
- return true;
- } else {
- error=true;
- output = output.concat( "- The Phone Number is not valid.\n");
- retun false;
- }
- }
- function testEmail(field){ //tests if contains an @ and "." in the correct position
- var atPosition= field.indexOf("@");
- var dotPosition= field.lastIndexOf(".");
- if (atPosition<1 || dotposition+2>=field.length || dotPosition<atPosition+2){
- error= true;
- output = output.concat("- Entered Email Addresses must be real.\n");
- return false;
- } else{
- return true;
- }// end else If (symbol test)
- }
- function testValidPassword(value){
- var number = /[0-9]/;
- var alphabet = /[a-zA-z]/;
- var containsNumber = new boolean(false);
- var containsAlpha = new boolean(false);
- for (x=0; x<=value.length; x++){
- if (alphabet.test(value[x])){
- containsAlpha = true;
- }else if(number.test(value[x])){
- containsNumber = true;
- }else {
- return false; //is not a valid password, halts function and returns false
- }//End if, elseif, else
- }//End for loop
- if ((value.length>=8)&& containsNumber && containsAlpha){
- return true;
- } else{
- output = output.concat("- Passwords must be atleast 8 alphanumeric\n characters long and have atleast one number\n");
- return false;
- }//End retuns
- }//End function
- function validation(this){//designed to be universal and work with any form
- var element = this.elements.length;//used as a reference for all elements inside of the form
- var error = new boolean(false);
- var output = new string("The following errors have occured:\n");
- for (x=0; x<=elements; x++){
- //name is assigned by developer and is set to a predefined "email, firstname, last name etc"
- var name=this.elements[x].name;
- var value=this.elements[x].value; //Entry withint text boxes etc
- switch(name){
- case "firstName":
- if ((!testEmpty("First Name", value))||(!testTextOnly("'First Name'", value))){
- error = true;
- }
- break;
- case "lastName":
- if ((!testEmpty("Last Name",value))||(!testTextOnly("'Last Name'", value))){
- error = true;
- }
- break;
- case "emailAddress":
- var confirmEmailAddress = this.getElemtentsByName("confirmEmailAddress").value;
- if ((!testEmpty("Email Address's",value))||(!string.compare(value, confirmEmailAddress))){
- error = true;
- output = output.concat("-Email fields do not match");
- }
- else if (!testEmail(value)){
- error = true;
- }
- break;
- /*No need to include a confirm email case because a mismatch takes priotiry over
- whether the email address is valid. ANd there is no need to test if the confirm
- email address is valid if the first is valid and the two match */
- case "phoneNumber":
- if ((!empty("Phone Number",value))||(!testPhoneNumber(value))){
- error=true;
- }
- break;
- case "password":
- var confirmPassword = new string(this.getElementsByName("confirmPassword").value);
- if ((!testEmpty("Password ",value))|| (!string.compare(value, confirmPassword))){
- x++;//skips the next element - if set up correctly it will be the confirm field
- error = true;
- output = output.concat( "- Passwords do not match.\n");
- }else if (!testValidPassword(value)){
- error = true;
- }
- break;
- //Testing has already been done, but they still need to be included in the "predefined list"
- case "confirmEmailAddress":
- case "confirmPassword":
- break;
- default:
- alert("There was a field that was not on the predefined list");
- break;
- }//End switch Statement
- }//End for loop
- alert(output);
- if (error){
- return false;//halts form and requires user to fix data
- } else {
- return true;//continues to next page
- }
- }//End Validation function
Advertisement
Add Comment
Please, Sign In to add comment