开发者

how to validate US currency with this format $123.45

开发者 https://www.devze.com 2023-01-06 08:28 出处:网络
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 th

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 ap开发者_开发知识库propriate regular expression for validating this value.. thanks in advance.


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.


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

or

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号