By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

regular expression problem

P: n/a
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

Jan 20 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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?)?$/

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.