Hi everyone, I want to be able to verify 2 fields in my website form: phone number: must be a 10 digit number; email address: must contain "@" Please provide me with the easiest way to do this validation. Thanks.
See Email validate function checkEmail(email) { if (email.length == 0) { window.alert(“You must provide an e-mail address.â€); return false; } if (email.indexOf(“/â€) > -1) { window.alert(“E-mail address has invalid character: /â€); return false; } if (email.indexOf(“:â€) > -1) { window.alert(“E-mail address has invalid character: :â€); return false; } if (email.indexOf(“,â€) > -1) { window.alert(“E-mail address has invalid character: ,â€); return false; } if (email.indexOf(“;â€) > -1) { window.alert(“E-mail address has invalid character: ;â€); return false; } if (email.indexOf(“@â€) < 0) { window.alert(“E-mail address is missing @â€); return false; } if (email.indexOf(“\.â€) < 0) { window.alert(“E-mail address is missing .â€); return false; } return true; } More see http://www.developerzone.biz/index.php?option=com_content&task=view&id=137&Itemid=45
Here's a form validation --------------- Javascript --------------- // Some Routines for HTML-Form Validation function trimXSpaces(Str){ if(!Str.length) return Str; while(Str.charAt(0) == ' ') Str = Str.substring(1, Str.length); if(!Str.length) return Str; while(Str.charAt(Str.length - 1) == ' ') Str = Str.substring(0, Str.length - 1); return Str; } function returnSelection(radioField) { var selection = null; for (i=0; i < radioField.length; i++) { if (radioField.checked) { selection=radioField.value; return selection; } } return selection; } // Check if Field contains something function ContainsSomething(Field) { if ((Field.type == "text") || (Field.type == "textarea") || (Field.type == "password") || (Field.type == "hidden")) { Str = trimXSpaces(Field.value); if (Str == "") { return false; } } else { if (returnSelection(Field) == null) { return false; } } return true; } // Check for valid (ie containg "@", ".", // and more than 6 characters) email-address in Field function IsValidEmail(Field) { if (!ContainsSomething(Field)) { return false; } if (Field.value.indexOf("@")==-1 || Field.value.indexOf(".")==-1 || trimXSpaces(Field.value).indexOf(" ")!=-1 || Field.value.length<6) { return false; } else { return true; } } // Check if Field contains a valid date of the form dd/mm/yy function IsValidDate(Field) { if (!ContainsSomething(Field)) { return false; } var indate=Field.value; var sdate = indate.split("/") var chkDate = new Date(Date.parse(indate)) var cmpDate = (chkDate.getMonth()+1)+ "/"+(chkDate.getDate())+ "/"+(chkDate.getYear()) var indate2 = (Math.abs(sdate[0]))+"/"+( Math.abs(sdate[1]))+ "/"+(Math.abs(sdate[2])) if (indate2 != cmpDate || cmpDate == "NaN/NaN/NaN") { return false } else { return true; } } // Check if Field contains numeric data only function IsNum(Field) { if (!ContainsSomething(Field)) { return false; } theNum = parseFloat(Field.value); if (Field.value != '' + theNum) { return false; } return true; } // Check if Field contains only letters function IsOnlyLetters(Field) { if (!ContainsSomething(Field)) { return false; } var Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ��" for (i=0; i < Field.value.length; i++) { var CheckChar = Field.value.charAt(i); CheckChar = CheckChar.toUpperCase(); if (Letters.indexOf(CheckChar) == -1) { return false; } } return true; } // Check if Field contains only digits in range Min to Max function IsInRange(Field, Min, Max) { if (IsNum(Field) == false) { return false; } if (Field.value < Min || Max < Field.value) { return false; } return true; } // Check if Field is not equal to strCompare function IsNotEqual(Field, strCompare) { if (Field.value== strCompare) { return false; } return true; } //compare two fields function IsNotEqual2(Field, Field2) { if (Field.value == Field2.value) { return false; } return true; } function isCreditCard(st) { if (st.length > 19) { return (false); } sum = 0; mul = 1; l = st.length; for (i = 0; i < l; i++) { digit = st.substring(l-i-1,l-i); tproduct = parseInt(digit ,10)*mul; if (tproduct >= 10) { sum += (tproduct % 10) + 1; } else { sum += tproduct; } if (mul == 1) { mul++; } else { mul--; } } if ((sum % 10) == 0) { return (true); } else { return (false); } } function IsVisa(cc) { if (((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4)) { return isCreditCard(cc); } return false; } function IsMasterCard(cc) { firstdig = cc.substring(0,1); seconddig = cc.substring(1,2); if ((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5))) { return isCreditCard(cc); } return false; } function IsAmericanExpress(cc) { firstdig = cc.substring(0,1); seconddig = cc.substring(1,2); if ((cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7))) { return isCreditCard(cc); } return false; } function IsValidCC(Field) { tempString = ""; bag = "- "; for (i = 0; i < Field.value.length; i++) { var c = Field.value.charAt(i); if (bag.indexOf(c) == -1) tempString += c; } cc = tempString; if (!isCreditCard(cc)) { return false; } if (!IsMasterCard(cc) && !IsVisa(cc) && !IsAmericanExpress(cc)) { return false; } return true; } //added by je // Check if Field contains only digits and does not start with 0 or 1 function IsValidIVR(Field) { if (IsNum(Field) == false) { return false; } if (Field.value.substring(0,1) == "0" || Field.value.substring(0,1) == "1") { return false; } return true; } function isInteger(inputVal) { inputStr = inputVal.toString(); for (var i = 0; i < inputStr.length; i++) { var oneChar = inputStr.charAt(i); if (oneChar < "0" || oneChar > "9") { if (i == 0 && oneChar == "-") { continue; } return false; } } return true; } // radio checker function ValidateRadio(Field) { myOption = -1; for (i=Field.choice.length-1; i > -1; i--) { if (Field.choice.checked) { myOption = i; } } if (myOption == -1) return false; else return true; } ----------------------------------------------------------------------------- Sample code: <script language="javascript" type="text/javascript"> function checkComplete(Form){ if(!ValidateRadio(Form)){ alert("Please Select a Choice."); return false; } if(!ContainsSomething(Form.firstname)){ alert("First Name should not be blank."); Form.firstname.focus(); return false; } if(!ContainsSomething(Form.lastname)){ alert("Last Name should not be blank."); Form.lastname.focus(); return false; } if(!ContainsSomething(Form.uname)){ alert("Username should not be blank."); Form.uname.focus(); return false; } if(!IsValidEmail(Form.email)){ alert("Email should not be blank or invalid."); Form.email.focus(); return false; } } </script>