Final Version 1.1, February 2002
This example presents the following functions for use in validating Integers:
These validation functions were taken from Eric Krock's FormChek.js, available from Netscape Communications Corporation. The wrapper functions validateInt and validateRange are mine; minor modifications to the FormChek routines have been made - namely, replacing the line 'return defaultEmptyOK' with 'return false.'
When validating integers, you need to consider the range of values that are appropriate. For example, the functions described here consider any whole number an integer. That is correct for math, but not for a computer - there are system-defined ranges that integer variables can have.
<form name="frmInput">
Enter something: <input name="txtInput" size="4">
<input type="button" value="Validate" onclick="validateInt()"></input>
<br />
<br />
Enter a range to check if the above is within:
<br />
<br />
from <input name="txtA" size="4">
to <input name="txtB" size="4">
<input type="button" value="Validate" onclick="validateRange()"></input>
</form>
function validateInt()
{
var o = document.frmInput.txtInput;
switch (isInteger(o.value))
{
case true:
alert(o.value + " is an integer")
break;
case false:
alert(o.value + " is not an integer")
}
}
function validateRange()
{
var s = document.frmInput.txtInput.value;
var A = document.frmInput.txtA.value;
var B = document.frmInput.txtB.value;
switch (isIntegerInRange(s, A, B))
{
case true:
alert(s + " is in range from " + A + " to " + B)
break;
case false:
alert(s + " is not in range from " + A + " to " + B)
}
}
// isIntegerInRange (STRING s, INTEGER a, INTEGER b)
function isIntegerInRange (s, a, b)
{ if (isEmpty(s))
if (isIntegerInRange.arguments.length == 1) return false;
else return (isIntegerInRange.arguments[1] == true);
// Catch non-integer strings to avoid creating a NaN below,
// which isn't available on JavaScript 1.0 for Windows.
if (!isInteger(s, false)) return false;
// Now, explicitly change the type to integer via parseInt
// so that the comparison code below will work both on
// JavaScript 1.2 (which typechecks in equality comparisons)
// and JavaScript 1.1 and before (which doesn't).
var num = parseInt (s);
return ((num >= a) && (num <= b));
}
function isInteger (s)
{
var i;
if (isEmpty(s))
if (isInteger.arguments.length == 1) return 0;
else return (isInteger.arguments[1] == true);
for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (!isDigit(c)) return false;
}
return true;
}
function isEmpty(s)
{
return ((s == null) || (s.length == 0))
}
function isDigit (c)
{
return ((c >= "0") && (c <= "9"))
}
The function isSignedInteger() returns true if all characters in the string are numbers; leading + or - is allowed. Used as support function for isPositiveInteger, isNonnegativeInteger, isNegativeInteger, and isNonpositiveInteger.
<form name="frmInput2"> Enter something: <input name="txtInputSignedInt" size="4"> <input type="button" value="Validate" onclick="validateSignedInt()"></input> </form>
function validateSignedInt()
{
var s = document.frmInput2.txtInputSignedInt.value;
switch(isSignedInteger(s))
{
case true:
alert(s + " is a signed integer");
break;
case false:
alert(s + " is not a signed integer");
}
}
function isSignedInteger (s)
{ if (isEmpty(s))
if (isSignedInteger.arguments.length == 1) return false;
else return (isSignedInteger.arguments[1] == true);
else {
var startPos = 0;
var secondArg = false;
if (isSignedInteger.arguments.length > 1)
secondArg = isSignedInteger.arguments[1];
// skip leading + or -
if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
startPos = 1;
return (isInteger(s.substring(startPos, s.length), secondArg))
}
}
// ...Plus isInteger, isEmpty, isDigit
The function isPositiveInteger returns true if the string is an integer greater than 0.
<form name="frmInput3"> Enter something: <input name="txtInputPosInt" size="4"> <input type="button" value="Validate" onclick="validatePosInt()"></input> </form>
function validatePosInt()
{
var s = document.frmInput3.txtInputPosInt.value;
switch(isPositiveInteger(s))
{
case true:
alert(s + " is a positive integer");
break;
case false:
alert(s + " is not a positive integer");
}
}
function isPositiveInteger (s)
{ var secondArg = false;
if (isPositiveInteger.arguments.length > 1)
secondArg = isPositiveInteger.arguments[1];
// The next line is a bit byzantine. What it means is:
// a) s must be a signed integer, AND
// b) one of the following must be true:
// i) s is empty and we are supposed to return true for
// empty strings
// ii) this is a positive, not negative, number
return (isSignedInteger(s, secondArg)
&& ( (isEmpty(s) && secondArg) || (parseInt (s) > 0) ) );
}
//...Plus isSignedInteger, isInteger, isEmpty, isDigit
The function isNonnegativeInteger returns true if the string is >= 0.
<form name="frmInput4"> Enter something: <input name="txtInputNNInt" size="4"> <input type="button" value="Validate" onclick="validateNNInt()"></input> </form>
function validateNNInt()
{
var s = document.frmInput4.txtInputNNInt.value;
switch(isNonnegativeInteger(s))
{
case true:
alert(s + " is non-negative");
break;
case false:
alert(s + " is not non-negative");
}
}
function isNonnegativeInteger (s)
{ var secondArg = false;
if (isNonnegativeInteger.arguments.length > 1)
secondArg = isNonnegativeInteger.arguments[1];
// The next line is a bit byzantine. What it means is:
// a) s must be a signed integer, AND
// b) one of the following must be true:
// i) s is empty and we are supposed to return true for
// empty strings
// ii) this is a number >= 0
return (isSignedInteger(s, secondArg)
&& ( (isEmpty(s) && secondArg) || (parseInt (s) >= 0) ) );
}
//...Plus isSignedInteger, isInteger, isEmpty, isDigit