richardsimnett
asked on
how to verify that input is an integer or float?
Hello,
I need to verify that user input from a textbox is a float or an integer. Non-numeric data has to be rejected. I need to do this in javascript.
Cheers,
Rick
I need to verify that user input from a textbox is a float or an integer. Non-numeric data has to be rejected. I need to do this in javascript.
Cheers,
Rick
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, a little VBScript mixup there: Here's the correct code:
<html>
<head>
<script language="JavaScript">
function boo() {
var fr = new RegExp("^[0-9]+\.[0-9]+$") ;
var ir = new RegExp("^[0-9]+$");
var x = "12.50";
if (fr.exec(x) != null) {
alert("Float!");
} else if (ir.exec(x) != null) {
alert("Integer!");
} else {
alert("Invalid number!");
}
}
</script>
</head>
<body onload="javascript:boo();" >
</body>
</html>
<html>
<head>
<script language="JavaScript">
function boo() {
var fr = new RegExp("^[0-9]+\.[0-9]+$")
var ir = new RegExp("^[0-9]+$");
var x = "12.50";
if (fr.exec(x) != null) {
alert("Float!");
} else if (ir.exec(x) != null) {
alert("Integer!");
} else {
alert("Invalid number!");
}
}
</script>
</head>
<body onload="javascript:boo();"
</body>
</html>
:etmendz
Hi
I believe your first code didn't work cause you used Test instead of test.
I was thinking would you not want to check for an integer first. The way you have it an integer would match your float regexp?
Or instead just use
if ((fr.Test(x)) || (ir.Test(x))) {
alert("Number!");
} else {
alert("Invalid number!");
}
also your regular expressions don't allow for negative numbers
Cheers
Scott
Hi
I believe your first code didn't work cause you used Test instead of test.
I was thinking would you not want to check for an integer first. The way you have it an integer would match your float regexp?
Or instead just use
if ((fr.Test(x)) || (ir.Test(x))) {
alert("Number!");
} else {
alert("Invalid number!");
}
also your regular expressions don't allow for negative numbers
Cheers
Scott
To support signed numbers:
To match float: /^[+\-]?[0-9]+\.[0-9]+$/
To match integer: /^[+\-]?[0-9]$/
var fr = new RegExp("^[+\-]?[0-9]+\.[0- 9]+$");
var ir = new RegExp("^[+\-]?[0-9]$");
var x = "-12.50";
if (fr.exec(x) != null) {
alert("Float!");
} else if (ir.exec(x) != null) {
alert("Integer!");
} else {
alert("Invalid number!");
}
Have fun.
To match float: /^[+\-]?[0-9]+\.[0-9]+$/
To match integer: /^[+\-]?[0-9]$/
var fr = new RegExp("^[+\-]?[0-9]+\.[0-
var ir = new RegExp("^[+\-]?[0-9]$");
var x = "-12.50";
if (fr.exec(x) != null) {
alert("Float!");
} else if (ir.exec(x) != null) {
alert("Integer!");
} else {
alert("Invalid number!");
}
Have fun.
Just use "isNaN"
Greetz, Tys
Greetz, Tys
function ValidateString(objName)
{
var numberfield = objName;
if (chkNumeric(objName) == false)
{
//numberfield.select();
//numberfield.focus();
return false;
}
else
{
return true;
}
}
function chkNumeric(objName)
{
var checkOK = "0123456789";
//checkOK = checkOK + chr(8) + chr(127);
var checkStr = objName;
var allValid = true;
//alert("Len of object " + checkStr.value.length);
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 (!allValid)
{
alertsay = "Please enter only these values ";
alertsay = alertsay + checkOK ;
//alert(alertsay);
return (false);
}
}
Can check like
f (ValidateString(frmname.al l["filed"] .value)==f alse)
{
var numberfield = objName;
if (chkNumeric(objName) == false)
{
//numberfield.select();
//numberfield.focus();
return false;
}
else
{
return true;
}
}
function chkNumeric(objName)
{
var checkOK = "0123456789";
//checkOK = checkOK + chr(8) + chr(127);
var checkStr = objName;
var allValid = true;
//alert("Len of object " + checkStr.value.length);
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 (!allValid)
{
alertsay = "Please enter only these values ";
alertsay = alertsay + checkOK ;
//alert(alertsay);
return (false);
}
}
Can check like
f (ValidateString(frmname.al
The advantage of facilitating the validation of numbers and their format may be useful when you want to have reusable javascript functions to specifically validate float for money or percent fields and integer for quantity fields for example.
Although isNaN may be enough to validate a numeric entry, it does not, however, enforce the validation of numeric formats. You can achieve the same effect as isNaN with regular expressions but you can be more flexible and specific with the fomat as necessary.
To simply validate a number (float or integer) using regular expression and reject invalid characters, use the regular expression:
/^[0-9]+(\.[0-9]+)?$/
To support signed numbers:
/^[+\-]?[0-9]+(\.[0-9]+)?$ /
To support accounting format entries where "(#)" is for negative #:
/^([0-9]+(\.[0-9]+)?|\([0- 9]+(\.[0-9 ]+)?\))$/
Have fun...
Although isNaN may be enough to validate a numeric entry, it does not, however, enforce the validation of numeric formats. You can achieve the same effect as isNaN with regular expressions but you can be more flexible and specific with the fomat as necessary.
To simply validate a number (float or integer) using regular expression and reject invalid characters, use the regular expression:
/^[0-9]+(\.[0-9]+)?$/
To support signed numbers:
/^[+\-]?[0-9]+(\.[0-9]+)?$
To support accounting format entries where "(#)" is for negative #:
/^([0-9]+(\.[0-9]+)?|\([0-
Have fun...
To match float: /^[0-9]+\.[0-9]+$/
To match integer: /^[0-9]$/
Test for float first. If that fails, then test for integer. If that fails, there are invalid characters in the text box.
var fr = new RegExp("^[0-9]+\.[0-9]+$")
var ir = new RegExp("^[0-9]+$");
if (fr.Test(x)) {
alert("Float!");
} else if (ir.Test(x)) {
alert("Integer!");
} else {
alert("Invalid number!");
}
Have fun.