Sunday, December 10, 2006

Function which allows only integers...

function IsInteger(varInt)
{
var checkOK = "0123456789";
var checkStr = varInt;
var allValid = true;
var allNum = "";
for (i = 0; i < checkStr.length; i++)
{
ch = checkStr.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
if (ch != ",")
allNum += ch;
}
return allValid;
}

//--------------------

//how to use

if (document.Form1.txtMainTelephone.value!="")
{
if (!IsInteger(document.Form1.txtMainTelephone.value))
{
alert("Invalid character entered in main telephone number!\nPlease enter only numbers.");
document.Form1.txtMainTelephone.focus();
return false;
}
}

1 comment:

Rajesh Sivaraman said...

@ that time I was not aware of "isNaN" JS function :D
Check this:
http://www.w3schools.com/jsref/jsref_isNaN.asp