Problem Description
These are reusable functions for some common number validations.
Solution Description
Here are four simple javascript functions which help in number validations.
isBlank(inputValue) - Returns true if the input value is whitespace, newlines, or
tabs. Otherwise false.
isNumeric(inputValue) - Returns true if the input value is a number, otherwise false.
isPositiveNumber(inputValue) - Returns true if the input value is a positive number
(2.7, 9.89), otherwise false.
isSingleDigitNumber(inputValue) - Returns true if the value is a single character
0-9
Code Snippet
<HTML>
<HEAD>
<!-- Copy and Paste this code into the HEAD of your HTML document -->
<SCRIPT TYPE="text/javascript">
//................................................................
// isBlank(inputValue)
// Returns true if inputValue only contains spaces
//................................................................
function isBlank(inputValue){
if(inputValue == null)
{
return true;
}
for(var i=0;i<inputValue.length;i++) {
if ((inputValue.charAt(i)!=' ') && (inputValue.charAt(i)!="\t")
&& (inputValue.charAt(i)!="\n") && (inputValue.charAt(i)!="\r"))
{
return false;
}
}
return true;
}
//................................................................
//This function checks if the value is a number or not
//Returns true if the value is a number (-5.78, 1.98, 10)
//Returns false if the input contains text and special characters
//................................................................
function isNumeric(sText)
{
if(isBlank(sText))
return false;
var ValidChars = "0123456789.-";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}
//................................................................
//This function checks if the value is a positive number or not
//Returns true if the value is a number (1.98, 10)
//Returns false if the input is a negative number or
//contains text and special characters
//................................................................
function isPositiveNumber(sText)
{
if(isBlank(sText))
return false;
var ValidChars = "0123456789.";
var IsNumber=true;
var Char;
for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}
//................................................................
// isSingleDigitNumber(value)
// Returns true if number is a 1-character digit
//................................................................
function isSingleDigitNumber(inputNumber) {
if(isBlank(inputNumber))
return false;
if (inputNumber.length>1)
{
return false;
}
var ValidChars = "1234567890";
if (ValidChars.indexOf(inputNumber)!=-1)
{
return true;
}
return false;
}
</SCRIPT>
</HEAD>
<BODY>
<!-- Use the above javascript code as shown below.-->
<FORM>
<TABLE WIDTH=100% BORDER=1 CELLSPACING=0 CELLPADDING=2>
<TR>
<TD ALIGN="center"><B>Function</B></TD>
<TD ALIGN="center"><B>Description</B></TD>
<TD ALIGN="center"><B>Test</B></TD>
</TR>
<TR BGCOLOR="#eeeeee">
<TD VALIGN="top" ALIGN="left">isBlank</TD>
<TD VALIGN="top" ALIGN="left">True if the value is whitespace,
newlines, or tabs. Otherwise false.</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT TYPE="TEXT" NAME="blankValue"> <INPUT TYPE="BUTTON"
NAME="checkBlank" VALUE="isBlank" onclick="alert(isBlank(document.forms[0].blankValue.value));">
</TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">isNumeric</TD>
<TD VALIGN="top" ALIGN="left">True if the value is a number,
otherwise false.</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT TYPE="TEXT" NAME="numericValue"> <INPUT TYPE="BUTTON"
NAME="checkNumeric" VALUE="isNumeric" onclick="alert(isNumeric(document.forms[0].numericValue.value));">
</TD>
</TR>
<TR BGCOLOR="#eeeeee">
<TD VALIGN="top" ALIGN="left">isPositiveNumber</TD>
<TD VALIGN="top" ALIGN="left">True if the value is a positive
number (2.7, 9.89), otherwise false.</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT TYPE="TEXT" NAME="positiveValue"> <INPUT TYPE="BUTTON"
NAME="checkPositive" VALUE="isPositiveNumber" onclick="alert(isPositiveNumber(document.forms[0].positiveValue.value));">
</TD>
</TR>
<TR>
<TD VALIGN="top" ALIGN="left">isSingleDigitNumber</TD>
<TD VALIGN="top" ALIGN="left">Returns True if the value
is a single character 0-9</TD>
<TD VALIGN="top" ALIGN="left">
<INPUT TYPE="TEXT" NAME="singleDigitValue"> <INPUT TYPE="BUTTON"
NAME="checkSingleDigit" VALUE="isSingleDigitNumber" onclick="alert(isSingleDigitNumber(document.forms[0].singleDigitValue.value));">
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>