Joined: Fri Jun 26, 2009 5:50 am Posts: 23 Has thanked: 0 time Have thanks: 1 time
Hi, I am currently validating a form client side only using javascript. People have started submitting blank forms by disabling javascript in there browser so i would now like to add some server side validation. Here is the code i am using. Any suggestions are welcome.
Thanks
<script type="text/JavaScript"> <!-- function ValidateForm (FormName) { // check that a title has been selected if (FormName.Title.selectedIndex < 0) { alert("Error - You must select a title"); FormName.Title.focus(); return (false); }
// check if the first drop down is selected, if so, invalid selection if (FormName.Title.selectedIndex == 0) { alert("Error - You must select a title"); FormName.Title.focus(); return (false); }
// check to see if the first name is blank if (FormName.FirstName.value == "") { alert("Error - You must enter a first name."); FormName.FirstName.focus(); return (false); }
// check to see if the first name is blank if (FormName.Surname.value == "") { alert("Error - You must enter a surname."); FormName.Surname.focus(); return (false); }
// check if email field is blank if (FormName.Email.value == "") { alert("Error you must enter a valid email address."); FormName.Email.focus(); return (false); }
// Check email address contains @ and . var chkEmail = "@."; var chkStr = FormName.Email.value; var EmailValid = false; var EmailAt = false; var EmailPeriod = false; for (i = 0; i < chkStr.length; i++) { ch = chkStr.charAt(i); for (j = 0; j < chkEmail.length; j++) { if (ch == chkEmail.charAt(j) && ch == "@") EmailAt = true; if (ch == chkEmail.charAt(j) && ch == ".") EmailPeriod = true; if (EmailAt && EmailPeriod) break; if (j == chkEmail.length) break; } // Found @ and . in the email address if (EmailAt && EmailPeriod) { EmailValid = true break; } } if (!EmailValid) { alert("Error - The email address must contain a @ and a ."); FormName.Email.focus(); return (false); } // check to see if the contact telephone is blank if (FormName.Telephone.value == "") { alert("Error - You must enter a contact telephone number."); FormName.Telephone.focus(); return (false); }
// check that the agree terms has been selected if (!FormName.AgreeTerms.checked) { alert("Error - You must agree to the terms and conditions"); return (false); } // check to see thatthe spam filter has been entered if (FormName.SpamFilter.value == "") { alert("Error - Please enter the spam filter "); FormName.SpamFilter.focus(); return (false); } // check to see that the spam filter is correct if (FormName.SpamFilter.value != "19") { alert("Error - Incorect answer for the spam filter 20+6-7="); FormName.SpamFilter.focus(); return (false); } return (true); } //--> </script>