This time we'll make a form that collects information about the
visitor at your site. You must have filled-in copious
registration forms or survey forms where you had to enter your
name, your email, your address, etc. Sometimes users,
intentionally or unintentionally, enter wrong information that
can either spoil your database scheme or give you lots of useless
data and hence, waste your precious server space.
To avoid such problems, as much as it can be managed, we
programmatically try to make sure, that data is entered in an
orderly fashion, and no unusable fields are entered. Checking
individual fields of the form does this.
We'll see a form here with three fields: Name, Phone and Email.
In this form, no field should be left blank, there should be no
numbers in the Name field [1,2,3,4,.], and in the Email field, no
email should be without the "@" sign. We can carry out more
complex validations, but at the moment, these three should
suffice.
/// Remove the extra dots while testing. They have been just
inserted so that some email programs don't freak out at the
presence of a JavaScript in the email.
<..script language="JavaScript1.2">
function CheckName(HoldName)
{
NoNumThere='true';
for(i=0; i
for(j=0; j<10; j++)
{
if(HoldName.charAt(i)==j.toString())
{
NoNumThere='false';
break;
}
}
if(NoNumThere=='false')
{
break;
}
}
return NoNumThere;
}
function CheckMail(HoldMail)
{
IsValid='true';
if(HoldMail.indexOf("@")<=0)
{
IsValid='false';
}
return IsValid;
}
function checkfields()
{
var AllFilled='true';
for(i=0; i<3; i++)
{
if(visitor.elements[i].value.length==0)
{
alert("The field [" + visitor.elements[i].name + "] can not be
left blank.");
AllFilled='false';
visitor.elements[i].focus;
return;
}
}
if(AllFilled=='true')
{
var NameValid=true;
var EmailValid=true;
NameValid=CheckName(visitor.vname.value);
EmailValid=CheckMail(visitor.vemail.value);
if(NameValid=='false')
{
alert("Sorry, your name can not contain numbers.");
visitor.vname.focus;
}
if(EmailValid=='false')
{
alert("Sorry, this does not seem like a valid email
address.");
}
}
if(NameValid=='true' & EmailValid=='true')
{
alert("RIGHTO!!!");
}
}