Click to See Complete Forum and Search --> : Checking if number


IxxI
03-18-2003, 05:44 AM
I'm sure that this has been asked before but I can't find it. How would you check a form text input to see that it has only had a number entered in it and nothing else.
Thanks,

IxxI

khalidali63
03-18-2003, 06:26 AM
Presuming that the form has a text field
t1

var t1Val = document.formName.t1.value;

//confirm that value is a number

var numA=0;

if(isNaN(parseInt(t1Val)){
alert("Text 1 field 1 has non number value")
}else{
numA = parseInt(t1Val);
}

hope this helps

Cheers

Khalid

IxxI
03-18-2003, 07:55 AM
Cheers Khalid,
but it doesn't seem to work. I've tried it as part of my code and standalone (exactly as you wrote it) but nothing happens even when I type in a non-number.
Any ideas?

IxxI

IxxI
03-18-2003, 08:13 AM
Tis OK I've done it a different way. In case you're interested:

var number=document.form.t1.value;
number++;
if (isNaN(number)
{alert("You must type a NUMBER in!");
return false;}
else{number--;}

Fairly horrible but it works!!

IxxI

khalidali63
03-18-2003, 09:54 AM
Originally posted by IxxI
Tis OK I've done it a different way. In case you're interested:
....

No thanks..

I just don't understand why would you want to go through the trouble of
number++
and number--

where as isNaN(parseInt(number))
would do just that...??

anyways..

Cheers

Khalid

IxxI
03-18-2003, 11:00 AM
I did try to use your code, but as I said I couldn't get it to work. I don't know why but even as a standalone scrap of code with just one textbox on a page it didn't work. If you can explain why then I'll happily use it, its just that at the moment I can't. Sorry.

IxxI

AdamBrill
03-18-2003, 12:10 PM
Here is a modification of Khalid's code that works:
var t1Val = document.formName.t1.value;

//confirm that value is a number

var numA=0;

if(isNaN(parseInt(t1Val))){
alert("Text 1 field 1 has non number value");
return false;
}else{
numA = parseInt(t1Val);
} Khalid, you forgot a ) in the if statment...