|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Simple Regular Expression matching - Currency
Just need a regular expression to match a currency entered by a user.
The acceptable formats for $100.00 would be: 100.00 100.0 100. 100 I am using this regular expression: /\d+(\.\d{0,2})?/ But it's letting by things like: 100.... 100....00 Being a novice in expression matching, I'm not sure where I'm going wrong. I thought putting \. would ask for exactly one '.' |
|
#2
|
||||
|
||||
|
This may be of help...
__________________
There are 10 types of people in this world - those who understand binary and those who don't... PHP | MySQL | DevShed Forum Search | Google Search |
|
#3
|
||||
|
||||
|
100....
100....00 For both of these, it matches the \d+ part (with 100). After that it looks for \.\d{0,2} but fails. However it sees the ?, realises that the last bit is optional and returns true. Make use of the 'start' and 'end' characters (^ and $ respectively). Also, use a non-capturing group (?:blah) rather than (blah) - it's more efficient if you don't want to refer to them later, which it seems you don't in this case. Rewritten: Code:
/^\d+(?:\.\d{0,2})?$/
|
|
#4
|
|||
|
|||
|
This matches US currency
// max character length $99,888,777,666,555.4444
// same as 99888777666555.4444 ^[$]?([0-9][0-9]?([,][0-9]{3}){0,4}([.][0-9]{0,4})?)$|^[$]?([0-9]{1,14})?([.][0-9]{1,4})$|^[$]?[0-9]{1,14}$ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Simple Regular Expression matching - Currency |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|