Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Form Input Pattern Currency Format

Tags:

html

regex

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?

like image 984
user748500 Avatar asked May 11 '11 11:05

user748500


People also ask

How do you write an input tag pattern?

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.


1 Answers

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 
like image 93
Andrey Santrosyan Avatar answered Sep 22 '22 21:09

Andrey Santrosyan