Regex help!!!

D

DbZ

Hi, I really need the help of a regex genius!!!

I'm a bit new to regex, just started to dabble a bit in it. I'm busy
writing a financial app where i have to validate a couple of fields
according to currency. Now - that being said - I have kind of figured
out a little on my own.

I have the following regex validating the field -

/^\d+(\.\d{2})?$/ - which looks for a value of 1000000.00 or
similar

If someone could give me some advice on tweaking it for allowing
spaces
for e.g. 1 000 000.00

Basically it needs to check for digit grouping as above, but also
allow for normal input
ie - 1000000.00

If anyone could help me it would be really great!!!! and you would
probably make my day.

Thank You

dbz
 
E

Evertjan.

DbZ wrote on 01 aug 2007 in comp.lang.javascript:
Hi, I really need the help of a regex genius!!!

I'm a bit new to regex, just started to dabble a bit in it. I'm busy
writing a financial app where i have to validate a couple of fields
according to currency. Now - that being said - I have kind of figured
out a little on my own.

I have the following regex validating the field -

/^\d+(\.\d{2})?$/ - which looks for a value of 1000000.00 or
similar

/^([1-9]\d*)?\d(\.\d\d)?$/

will prevent starting zeros but the last one
If someone could give me some advice on tweaking it for allowing
spaces
for e.g. 1 000 000.00

Basically it needs to check for digit grouping as above, but also
allow for normal input
ie - 1000000.00

/^([1-9][\d ,]*)?\d(\.\d\d)?$/

allows for spaces or commas nearly anywhere.

Do you want to allow for triads only?

[NOT TESTED]
 
D

DbZ

RE: Evertjan.
The Netherlands.


Thank You very much - It seems to have sorted out my problem.
dbz
 
R

Rik

DbZ wrote on 01 aug 2007 in comp.lang.javascript:
Hi, I really need the help of a regex genius!!!

I'm a bit new to regex, just started to dabble a bit in it. I'm busy
writing a financial app where i have to validate a couple of fields
according to currency. Now - that being said - I have kind of figured
out a little on my own.

I have the following regex validating the field -

/^\d+(\.\d{2})?$/ - which looks for a value of 1000000.00 or
similar

/^([1-9]\d*)?\d(\.\d\d)?$/

will prevent starting zeros but the last one
If someone could give me some advice on tweaking it for allowing
spaces
for e.g. 1 000 000.00

Basically it needs to check for digit grouping as above, but also
allow for normal input
ie - 1000000.00

/^([1-9][\d ,]*)?\d(\.\d\d)?$/

Hmmm, 0.15, not valid? Or 0 for that matter?
allows for spaces or commas nearly anywhere.

Do you want to allow for triads only?

Which would be something like:
Enforcing the space or ,:
/^([1-9]\d{0,2}(( |,)\d{3})*?|0)(\.\d\d)?$/
Allowing for the space or ,:
/^([1-9]\d{0,2}(( |,)?\d{3})*?|0)(\.\d\d)?$/
 
E

Evertjan.

Rik wrote on 01 aug 2007 in comp.lang.javascript:
/^([1-9][\d ,]*)?\d(\.\d\d)?$/

Hmmm, 0.15, not valid? Or 0 for that matter?

Eh?

<script type='text/javascript'>
alert(/^([1-9][\d ,]*)?\d(\.\d\d)?$/.test('0.15'));
alert(/^([1-9][\d ,]*)?\d(\.\d\d)?$/.test('0'));
</script>

Both return true, as expected [not by you].

;-)
 
R

Rik

Rik wrote on 01 aug 2007 in comp.lang.javascript:
/^([1-9][\d ,]*)?\d(\.\d\d)?$/

Hmmm, 0.15, not valid? Or 0 for that matter?

Eh?

You're totally right
<script type='text/javascript'>
alert(/^([1-9][\d ,]*)?\d(\.\d\d)?$/.test('0.15'));
alert(/^([1-9][\d ,]*)?\d(\.\d\d)?$/.test('0'));
------------^----------^---^

Maybe I was fooled by the simple genius of these, or I assumed the \d
right before the optional decimal to have a + or *, don't know what
exactly went wrong. Perhaps it was because I allready implemented the
triad in my mind, in which case this construct would not be possible,
because the first part would have to keep count of those groups of three..

However I came to my false accusation, please accept my humble apologies:)
 
E

Evertjan.

Rik wrote on 01 aug 2007 in comp.lang.javascript:
Rik wrote on 01 aug 2007 in comp.lang.javascript:
/^([1-9][\d ,]*)?\d(\.\d\d)?$/

Hmmm, 0.15, not valid? Or 0 for that matter?

Eh?

You're totally right
<script type='text/javascript'>
alert(/^([1-9][\d ,]*)?\d(\.\d\d)?$/.test('0.15'));
alert(/^([1-9][\d ,]*)?\d(\.\d\d)?$/.test('0'));
------------^----------^---^

Maybe I was fooled by the simple genius of these, or I assumed the \d
right before the optional decimal to have a + or *, don't know what
exactly went wrong. Perhaps it was because I allready implemented the
triad in my mind, in which case this construct would not be possible,
because the first part would have to keep count of those groups of
three.

However I came to my false accusation, please accept my humble
apologies :)

OK.
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top