Question

I need to use regular expression for to validate the following amount formatts from the input $10.00 0r ($10.00) ... that is amount should be in R2 format with $ sign with or without bracket. If it is in other formats validation has to fail. Please suggest on this.

Was it helpful?

Solution

UPDATE:
Based on the comments below and the comment in the question about R2 format, the modified regex would be:

((\($\d+\.\d{2}\))|($\d+\.\d{2}))

OLDER ANSWER:
Try this regex:

\$\s*\(?\d+(\.\d+)?\)?

\$ - matches the dollar symbol
\s* - matches any white space betn the dollar symbol and the digits or brace
\(? - matches the optional left brace
\d+ - matches the integral part of the number
(\.\d+)? - matches the optional decimal portion with the dot
\)? - matches the optional right brace

So you can try this xsd validator:

<xs:simpleType name="CurrencyFormat">
    <xs:restriction base="xs:string">
        <xs:pattern value="\$\s*\(?\d+(\.\d+)?\)?" />
    </xs:restriction>
</xs:simpleType>

OTHER TIPS

try this:

^(\(\$\d+\.\d{2}\)|\$\d+\.\d{2})$
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top