[thelist] numeric only regex?

Christian Heilmann codepo8 at gmail.com
Tue Aug 2 16:16:18 CDT 2005


On 8/2/05, Tom Dell'Aringa <pixelmech at yahoo.com> wrote:
> --- Frank <lists at frankmarion.com> wrote:
> > >Tom said:::
> > >Hi, I'm trying to come up with a simple regex that will allow me to test
> > >for numbers 0-9 ONLY.
> 
> > Depending on the language... try this:
> >
> > Assuming you want only one instance of 0 to 9:     [0-9]
> > Only two instances:     [0-9][0-9] OR [0-9]{2,}
> > Infinite instances:     [0-9]+
> > Zero or more instances:     [0-9]*
> > Zero or one instances:     [0-9]?
> 
> 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?

var ff=new RegExp("^\\d+$").test(numtext.value)

You need the delimiters (otherwise "bla300" would return true) and
when you use the regExp construct every backslash needs doubling.


-- 
Chris Heilmann 
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/  
Binaries: http://www.onlinetools.org/


More information about the thelist mailing list