Using HTML5 I have an input field that should validate against a dollar amount entered. Currently I have the following markup:
<input type="number" pattern="(\d{3})([\.])(\d{2})">
This works great for an amount that is greater than 100.00 and less than 1,000.00. I am trying to write the pattern (regex) to accept different dollar amounts. Maybe upwards of 100,000.00. Is this possible?
The pattern attribute specifies a regular expression that the <input> element's value is checked against on form submission. Note: The pattern attribute works with the following input types: text, date, search, url, tel, email, and password. Tip: Use the global title attribute to describe the pattern to help the user.
The best we could come up with is this:
^\\$?(([1-9](\\d*|\\d{0,2}(,\\d{3})*))|0)(\\.\\d{1,2})?$
I realize it might seem too much, but as far as I can test it matches anything that a human eye would accept as valid currency value and weeds out everything else.
It matches these:
1 => true 1.00 => true $1 => true $1000 => true 0.1 => true 1,000.00 => true $1,000,000 => true 5678 => true
And weeds out these:
1.001 => false 02.0 => false 22,42 => false 001 => false 192.168.1.2 => false , => false .55 => false 2000,000 => false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With