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.
<script type="text/javascript">
<!--
var whitespace = " \t\n\r";/* Define validations to run */
validations = new Array();// Search string looking for characters that are not whitespace
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;
for (i = 0; i < s.length; i++)// All characters are whitespace.
{
var c = s.charAt(i);
if (whitespace.indexOf(c) == -1)
return false;
}
return true;// Check if current character is number.
}
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++)
{
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>