NOTICE: This website is no longer updated or supported - as such many of the techniques used to build it may seem antiquated in the modern day. It is preserved for historical reasons only.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>Form Check</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">
<!--
var whitespace = " \t\n\r";
/* Define validations to run */
validations = new Array();
validations[0] = ["document.myform.username", "notblank"];
validations[1] = ["document.myform.useremail", "validemail"];
validations[2] = ["document.myform.favoritenumber", "isnumber"];
function isEmpty(s)
{
var i;
if((s == null) || (s.length == 0))
return true;
// Search string looking for characters that are not whitespace
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (whitespace.indexOf(c) == -1)
return false;
}
// All characters are whitespace.
return true;
}
function isEmail(field)
{
var positionOfAt;
var s = field.value;
if (isEmpty(s))
{
alert("Email may not be empty");
field.focus();
return false;
}
positionOfAt = s.indexOf('@',1);
if ( ( positionOfAt == -1) || (positionOfAt == (s.length-1)) )
{
alert("E-mail not in valid form!");
field.focus();
return false;
}
return true;
}
function isDigit(c)
{
return ((c > = "0") && (c <= "9"))
}
function isInteger(field)
{
var i, c;
var s = field.value;
if (isEmpty(s))
{
alert("Field cannot be empty");
field.focus();
return false;
}
for (i = 0; i < s.length; i++)
{
// Check if current character is number.
c = s.charAt(i);
if (!isDigit(c))
{
alert("Field must contain only digits");
field.focus();
return false;
}
}
return true;
}
function validate()
{
var i;
var checkToMake;
var field;
for (i = 0; i < validations.length; i++)
{
checkToMake = validations[i][1];
field = eval(validations[i][0]);
switch (checkToMake)
{
case 'notblank': if (isEmpty(field.value))
{
alert("Field may not be empty");
field.focus();
return false;
}
break;
case 'validemail' : if (!isEmail(field))
return false;
break;
case 'isnumber' : if (!isInteger(field))
return false;
}
}
return true;
}
//-->
</script>

</head>
<body>

<form name="myform" id="myform" method="get" action="http://www.htmlref.com" onsubmit="return validate();">
Username: <input type="text" name="username" id="username" size="30" />
<br />
Email: <input type="text" name="useremail" id="useremail" size="30" maxlength="30" />
<br />
Favorite number: <input type="text" name="favoritenumber" id="favoritenumber" size="10" maxlength="10" />
<br />
<input type="submit" value="Submit" />
</form>

</body>
</html>


CLOSE WINDOW | VIEW RENDERED PAGE