regular expression problem

L

lambertb

Hi all,

my knowledge on regular expression is average. I have this one in
asp.net :

^\d+(\.\d\d?)?

To my eyes, it correctly validates any whole number (ex: 1, 100), and
any decimal number with one or two decimals (42.0, 42.42, 420.42, etc.)

Why is in javascript, declared like that :

var regex = /^\d+(\.\d\d?)?/

outputs this :

regex.text('abc') // outputs false, correct
regex.test('42er') // outputs true, INCORRECT (chars are alpha)
regex.test('42.420') // outputs true, INCORRECT (3 digits after
point)

Anyone have suggestions please?

Thanks!

ibiza
 
E

Evertjan.

wrote on 20 jan 2006 in comp.lang.javascript:
Hi all,

my knowledge on regular expression is average. I have this one in
asp.net :

^\d+(\.\d\d?)?

To my eyes, it correctly validates any whole number (ex: 1, 100), and
any decimal number with one or two decimals (42.0, 42.42, 420.42, etc.)

Why is in javascript, declared like that :

var regex = /^\d+(\.\d\d?)?/

var regex = /^\d+(\.\d\d?)?$/
outputs this :

regex.text('abc') // outputs false, correct
regex.test('42er') // outputs true, INCORRECT (chars are alpha)
regex.test('42.420') // outputs true, INCORRECT (3 digits after
point)

This will include negative values:

var regex = /^-?\d+(\.\d\d?)?$/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top