Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate Numeric Value

Status
Not open for further replies.

stan8450

Programmer
May 3, 2002
16
CA
I am validating a form in Dreamweaver using JavaScript. I would like a user to only enter a numeric value in a text field.


i.e.
if (IsNumeric(document.calcform.SqFt.value) == "")
{
alert ( "Please enter in the approximate Square Footage of you house." );
document.calcform.SqFt.focus();
valid = false;
return;
thanx for any help
 
Call this function using the form element value as the argument:

function isNumeric(onevalue)
{
var digits='0123456789';
for (var oi=0;oi<onevalue.length;oi++)
{
var onechar=onevalue.charAt(oi)
if (digits.indexOf(onechar)==-1)
{
alert ( &quot;Please enter in the approximate Square Footage of your house.&quot; );
document.calcform.SqFt.focus();
return false;
}
}
return true;
}

 
there's a easier way using regexps.

Code:
var intnumber=/^\d+$/;
var floatnumber=/^\d+\.\d+$/;

if (!intnumber.exec(myform.myintfield.value) || !floatnumber.exec(myform.myfloatfield.value)){
  alert('Incorrect number');
}


 Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
there's a easier way using regexps.

Code:
var intnumber=/^\d+$/;
var floatnumber=/^\d+\.\d+$/;

if (!intnumber.exec(myform.myintfield.value) || !floatnumber.exec(myform.myfloatfield.value)){
  alert('Incorrect number');
}

Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
wouldn't it be test instead of exec?

var intnumber=/^\d+$/;
var floatnumber=/^\d+\.\d+$/;

if (!intnumber.test(myform.myintfield.value) || !floatnumber.test(myform.myfloatfield.value)){
alert('Incorrect number');
} Gary Haran
 
well ... with exec i know it works :) but as i'm not an expert in JS ... Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
exec returns an array with all found occurances. If your array is empty it is considered false so I guess exec works as well but test is faster. :) Gary Haran
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top