JavaScript Validation of Form Fields: Integers

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “”>

<html xmlns=””>


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

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


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; }




<br />

<br />

<form action=””>

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



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



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







Validated by W3C (2010) Markup Validation Service at .