Total members 11895 |It is currently Mon Dec 30, 2024 7:06 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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

javascript code
<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>





Author:
Newbie
User avatar Posts: 23
Have thanks: 1 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : AJAX form validation
 Validation using Ajax     -  
 Form Validation Problem     -  
 Form Validation Problem     -  
 complete registration form with validation     -  
 php login form (sign-in form)     -  
 What is AJAX, How to start AJAX?     -  
 DNS Validation     -  
 XML validation against DTD     -  
 date validation in asp     -  
 US Zip Code Validation     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com