regular expressions checks each character, hence there's no exact way to
see if the total value of the field is an integer.
You can do this:
var reDigit =3D /\d+$/; //what you did...
or maybe this:
var reDigit =3D /\d+[^.]$/;
or:
if(field.value.search(/\D+/) !=3D -1) alert("You can only fill in
digits...");
But I'd rather just use parseInt() or something to convert the value to
an integer.
I don't know if this helps you, or if I'm just rambling...
/Robert
-----Ursprungligt meddelande-----
Fr=E5n: Garrett Steed [mailto:gmsteed@s...]
Skickat: den 23 oktober 2002 20:18
Till: JavaScript HowTo
=C4mne: [javascript_howto] regular expressions
Not to long ago I sent a form validation example that looped through any
generic form and would check to see each form element was either
positive, had data in it, or was a number. To check if the form
elements was an number I used this:
if(isNaN(parseInt(domLocation.elements[i].value))){do this thing}
this works as long as the data doesn't resemble hex code. I had tried
using a regular expression like this:
var reDigit =3D /\d+$/;
if(domLocation.elements[i].value !=3D reDigit){do this thing}
My question is this. Can someone tell me the correct regular expression
to find out if the data in a form element is an int. And how to format
the "if" statement for that. I think I have tied everything but
checking for each alpha char. Thanks
Garrett
---
Improve your web design skills with these new books from Glasshaus.
Usable Web Menus
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=3Dnosim/theprogramm
e
r-20
Constructing Accessible Web Sites
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=3Dnosim/theprogramm
e
r-20
Practical JavaScript for the Usable Web
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=3Dnosim/theprogramm
e
r-20