Price input pattern html5

12,706

Solution 1

try this

[0-9]+(\\.[0-9][0-9]?)?

<form>
  <input type='number' pattern='[0-9]+(\\.[0-9][0-9]?)?' />
  <button type='submit'>Check</button>
</form>

Solution 2

The right answer is: [0-9]+(\.[0-9][0-9]?)?

Share:
12,706
emilan
Author by

emilan

Updated on August 09, 2022

Comments

  • emilan
    emilan over 1 year

    I've got input field and I want to check dot existence for value of price input field. The value will be look like this 12.00. Here is my code

    <g:textField class="span3" id="price" name="price" required="" pattern="\d+(\.\d{2})?"/>
    

    I write something like this \d+(\.\d{2})?, but received error. Any suggestions ?

  • Sujith S
    Sujith S about 6 years
    "11.11.11", will accept. i think it's not a valid price
  • Yasen
    Yasen about 4 years
    Hi, welcome to the SO club. I'd suggest to explain your answer
  • Jason Aller
    Jason Aller about 4 years
    When answering a seven year old question with other existing answers the burden is on you to clearly communicate what new aspect of the question your answer addresses. Code only answers can almost always benefit from the addition of some explanation, and regex answers need it even more.
  • Andy A.
    Andy A. almost 3 years
    Welcome to SO. Your regex allows a decimal operator without numbers behind! E.g. 012.. So I would use^[1-9][0-9]*(\.[0-9]{1,2})?$.