JavaScript Validation of Form Fields: Integers

<!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”>

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />

<title>JavaScript Validation of Form Fields: Integers</title>

<SCRIPT LANGUAGE=”JavaScript”>

function ValidateNumber(number) {

// setup list of acceptable characters

var Valid = “0123456789”;

// setup variable for checking each character var input;

// for each character input (up to the length of the input), check it against the valid characters list

for (i = 0; i < number.length; i++)

{ input = number.charAt(i); if (Valid.indexOf(input) == -1)

{

// if any single character is not on the valid character list, return an error message

alert(“You did not enter a number, please try again.”); return false; } }

// if all characters match the valid character list, return a success message

alert(“The number you have entered has been validated successfully”); return true; }

</script>

</head>

<body>

<br />

<br />

<form action=””>

<table border=”0″ cellspacing=”0″ cellpadding=”5″ style=”margin: auto;”>

<tr>

<td>

<span class=”style1″>Enter Number:</span>

</td>

<td>

<input type=”text” name=”number” size=”10″ value=”” onblur=”return ValidateNumber(number.value);” />

</td>

</tr>

</table>

</form>

</body>

</html>

Validated by W3C (2010) Markup Validation Service at .