views:

77

answers:

3

Hi I have one one web page which one one textbox for receiving the dollar value. My requirement is the user should insert the digit following by a $ sysmbol. And the second requirement is the user has the permission to insert only like this $123.45. Before the decimal point it should not exceed three digits and after the decimal point it should not exceed two digits. Please help me by providing the appropriate regular expression for validating this value.. thanks in advance.

+2  A: 

Try:

\$\d{1,3}(\.\d{1,2})?

\$ = a dollar, escaped as it is a special character
\d = a digit; {1,3} = between 1 and 3 repetitions
()? = an optional group:
\. = a dot (escaped) \d{1,2} = one or two digits

To play regular expressions (and test them) you can use Expresso or a similar tool.

Paolo Tedesco
If you dont want that there is stuff after or before the currency use ^\$\d{1,3}(\.\d{1,2})?$
Marks
$.59 would not be a valid amount with this regexp. I know I frequently drop 0s from the start, and I'd be annoyed if I had to put it in.
I82Much
@I82Much: I think from the question it looks like at least one digit is required; by the way, I would be annoyed to have to put a $ in front of the amount...
Paolo Tedesco
+2  A: 
\$\d{1,3}\.\d{1,2}

or

\$\d{1,3}\.\d{2}

if you want to force the last decimal to have 2 digits

RJD22
A: 

use regex...

http://regexlib.com/CheatSheet.aspx

Harsha M V