I think I created a working regular expression for what I need. Just wondering if anyone can break it or see a shorter way to write it.
The regular expression should validate the following...
- Dollar sign optional
- Negative numbers signified with parenthesis, not a minus
- If negative, dollar sign should be outside the parenthesis
- Commas are optional
- Max number is 999999.99
- Min number is (999999.99)
- Decimals do not have to be supplied, but if so, no more than two
digits
So here are some examples of valid ones...
9
$9
$0.99
($999,999.99)
(999999)
($999999)
(999,999)
99,999.9
This is what I have come up with:
^$?(((d{1,6}(.d{1,2})?)|(d{1,3},d{3}(.d{1,2})?)|(((d{1,6}(.d{1,2})?)|(d{1,3},d{3}(.d{1,2})?)))))$
CORRECTION, my spec was wrong, if the dollar sign is used it must be INSIDE the parenthesis.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…