Browse DevX
Sign up for e-mail newsletters from DevX


Automate Your Form Validation-4 : Page 4




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Write the Validation Rules
A validation script is only as good as the rules it contains. The remainder of this article lists validation functions for many common tasks. I present these functions as a starting point for you to build upon—they're not intended to be finished products. Many of them are relatively complete but some—noticeably the credit card rule—are not as robust as you might need for a production application. Also, the functions contain no built-in security; you'll need to implement security yourself.

The first three functions are relatively self-explanatory. The notEmpty() function accepts any value other than white space. The validateInteger() and validateFloat() functions ensure that the controls only accept numeric values by first parsing the string and then checking whether the result is numeric with the isNaN() method.

//Validation Rules function notEmpty(str){ if(strip(" \n\r\t",str).length ==0) return false; else return true; } function validateInteger(str){ str = strip(' \n\r\t',str); //remove leading zeros, if any while(str.length > 1 && str.substring(0,1) == '0'){ str = str.substring(1,str.length); } var val = parseInt(str); if(isNaN(val)) return false; else return true; } function validateFloat(str){ str = strip(' \n\r\t',str); //remove leading zeros, if any while(str.length > 1 && str.substring(0,1) == '0'){ str = str.substring(1,str.length); } var val = parseFloat(str); if(isNaN(val)) return false; else return true; }

The validateUSPhone(), validateSSN(), and validateZip() functions are similar in that they strip off all of the characters that people commonly enter for phone numbers. The functions then check the length of the resulting numeric character string. In addition, because Social Security numbers and Zip Codes are numeric, the validateSSN() and validateZip() functions also ensure that the entered data is a valid integer. Note that the validateUSPhone() function doesn't force phone numbers to be integers so that phone numbers represented by characters are passed through.

function validateUSPhone(str){ str = strip("*() -./_\n\r\t\\",str); if(str.length == 10 || str.length == 7) return true; else return false; } function validateSSN(str){ str = strip(" -.\n\r\t",str); if(validateInteger(str) && str.length == 9) return true; else return false; } function validateZip(str){ str = strip("- \n\r\t",str); if(validateInteger(str) && (str.length==9 || str.length==5)) return true; else return false; }

The credit card validation rule deserves a bit more attention. After stripping out garbage characters, I took advantage of the fact that all Visa card account numbers begin with a 4, all MasterCard accounts begin with a 5, all Discover card accounts begin with a 6, and all American Express cards begin with a 3. I got the card type from the drop-down list next to the credit card entry text field. If the first character isn't correct based on the type, the validateCC() function immediately returns false and the data fails the test. In addition, credit cards have a fixed number of digits: Amex accounts all contain 15 digits, and Visa, MasterCard, and Discover card accounts all contain 16 digits. Therefore, the function improves the validation by checking that the data begins with the correct number and is the correct length.

function validateCC(str,type){ str = strip("-./_\n\r\t\\",str); if(type=="1") if(str.charAt(0)!="4") return false; if(type=="2") if(str.charAt(0)!="5") return false; if(type=="3") if(str.charAt(0)!="6") return false; if(type=="4") if(str.charAt(0)!="3") return false; if(validateInteger(str) && ((str.length==15&&type=="4") || str.length==16)) return true; else return false; }

The validateDate() function first creates a Date object from the string. It then checks whether the valueOf() method of the Date object is numeric or non-zero. If the valueOf() method is non-numeric or zero, the string passed to the function does not represent a valid instance in time and, therefore, fails the test.

function validateDate(str){ var dateVar = new Date(str); if(isNaN(dateVar.valueOf()) || (dateVar.valueOf() ==0)) return false; else return true; }

The validateEMail() function strips out unwanted characters and then checks for the presence of both the "@" and "." characters. While it is possible to fool the rule, you'd be surprised at how many people will first attempt to avoid entering a valid e-mail address and then, after prompting, simply supply their e-mail address.

function validateEMail(str){ str = strip(" \n\r\t",str); if(str.indexOf("@") > -1 && str.indexOf(".") > -1) return true; else return false; } //End Validation Rules

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date