[thelist] numeric only regex?

Matt Warden mwarden at gmail.com
Tue Aug 2 19:10:53 CDT 2005


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tom Dell'Aringa wrote:
> I should have said, this is JavaScript. Based on the above, and I was trying something similar, I
> have this:
> 
> var ff = RegExp("[0-9]*").test(numtext.value)
> alert(ff)
> 
> But it's not right, I get true every time. We need to exclude alpha characters and special
> characters (like the *). My guess is my syntax is not quite right?

If you say [0-9]*, you're saying you want to match a number zero or
more times. Thus, everything will work here, even '*NKS^&' (matching a
number 0 times).

/^[0-9]+$/.test(varToTest)

This says: starting with the beginning of varToTest (^), match one or
more (+) characters in the range 0-9 and then match the end of
varToTest ($). Therefore, from start to end, varToTest must consist of
characters in the range 0-9.

hth,

- --
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFC8AuNAQ0d4HGyPE8RAgu5AJ9S/aaPDLELgqLdpq78XGRJTUrGUgCfU0tN
MZ9L+FYdp7UJuiG8jw2mQ9w=
=hlTQ
-----END PGP SIGNATURE-----


More information about the thelist mailing list