Search code examples
jqueryhtmlregular-language

HTML Input field pattern for numerical values.


I have an input field that I am using to get a monetary amount from a user. I am using a jQuery Money Mask function to put a mask over the user's input. So for example if the user types "100000" it will show in the input field as "$ 1,000.00". The issue is that I have a pattern setup for the input field to restrict it to only numerical values. I need to modify this regular expression (pattern="[0-9]*") so that it accepts monetary strings. Any help is most appreciated!

Code for the input field:

<input name="rentPayment[paymentAmount]" id="paymentAmount" placeholder="0.00" value="" type="text" pattern="[0-9]*">


Solution

  • I am not familiar with JQuery Money Mask but if all you need is regex then following should be enough to allow monetary values.

    pattern="[0-9$,.]*" - would allow 0-9 dollar sign, comma and period zero or more times.