05 HTML Form Validation
05 HTML Form Validation
Form validation used to occur at the server, after the client had
entered all necessary data and then pressed the Submit button. If
some of the data that had been entered by the client had been in
the wrong form or was simply missing, the server would have to
send all the data back to the client and request that the form be
resubmitted with correct information. This was really a lengthy
process and over burdening server.
<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
<!--
// Form validation code will come here.
//-->
</script>
</head>
<body>
<form action="/cgi-bin/test.cgi" name="myForm"
onsubmit="return(validate());">
<table cellspacing="2" cellpadding="2"
border="1">
<tr>
<td align="right">Name</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td align="right">EMail</td>
<td><input type="text" name="EMail" /></td>
</tr>
<tr>
<td align="right">Zip Code</td>
<td><input type="text" name="Zip" /></td>
</tr>
<tr>
<td align="right">Country</td>
<td>
<select name="Country">
<option value="-1" selected>[choose
yours]</option>
<option value="1">USA</option>
<option value="2">UK</option>
<option value="3">INDIA</option>
</select>
</td>
</tr>
<tr>
<td align="right"></td>
<td><input type="submit" value="Submit"
/></td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
<!--
// Form validation code will come here.
function validate()
{
if( document.myForm.Name.value == "" )
{
alert( "Please provide your name!" );
document.myForm.Name.focus() ;
return false;
}
if( document.myForm.EMail.value == "" )
{
alert( "Please provide your Email!" );
document.myForm.EMail.focus() ;
return false;
}
if( document.myForm.Zip.value == "" ||
isNaN( document.myForm.Zip.value ) ||
document.myForm.Zip.value.length != 5
)
{
alert( "Please provide a zip in the format
#####." );
document.myForm.Zip.focus() ;
return false;
}
if( document.myForm.Country.value == "-1" )
{
alert( "Please provide your country!" );
return false;
}
return( true );
}
//-->
</script>
Data Format Validation:
Now we will see how we can validate our entered form data
before submitting it to the web server.
<script type="text/javascript">
<!--
function validateEmail()
{
var emailID = document.myForm.EMail.value;
atpos = emailID.indexOf("@");
dotpos = emailID.lastIndexOf(".");
if (atpos < 1 || ( dotpos - atpos < 2 ))
{
alert("Please enter correct email ID")
document.myForm.EMail.focus() ;
return false;
}
return( true );
}
//-->
</script>
Before you ask, and someone always does, these examples will
currently work only in the following browsers: Safari 5, Chrome 6,
Opera 9, Firefox 4 Beta and the iPhone/iPad. Also each browser
has a slightly different default behaviour.
The simplest change you can make to your forms is to mark a text
input field as 'required':
Your Name:
This informs the (HTML5-aware) web browser that the field is to
be considered mandatory. Different browsers may mark the input
box in some way (Firefox 4 Beta adds a red box-shadow by
default), display a warning (Opera) or even prevent the form from
being submitted if this field has no value. Hopefully these
behaviours will converge in future releases.
Before you type anything into the box a red marker is shown. As
soon as a single character has been entered this changes to a
green marker to indicate that the input is 'valid'.
Using CSS you can place markers inside or alongside the input
box, or simply use background colours and borders as some
browsers do by default.
INPUT type="email"
Note that for this example we've made use of another HTML5
attribute placeholder which lets us display a prompt or
instructions inside the field - something that previously had to be
implemented using messy onfocus and onblur JavaScript events.
Email Address:
Again, different browsers implement this differently. In Opera it's
sufficient to enter just *@* for the input to be accepted. In Safari,
Chrome and Firefox you need to enter at least *@-.-. Obviously
neither example is very limiting, but it will prevent people from
entering completely wrong values, such as phone number, strings
with multiple '@'s or spaces.
INPUT type="url"
Website:
This time the minimum requirement for most browsers is one or
more letters followed by a colon. Again, not very helpful, but it
will stop people trying to input their email address or other such
nonsense.
pattern="https?://.+">
max="99" value="21"><br>
and in Opera:
INPUT type="password"
The tel input type is handy for the iPhone as it selects a different
input keyboard. There is no pattern-matching set by default so
you would have to implement that yourself using
the pattern attribute to accept only certain characters.