How to use Pattern to delimit a minimum number in an input text?

Asked

Viewed 724 times

1

Hello, I need to define that the minimum number inserted in an input of type text is the number 5, I tried as follows:

<input type="text" name="someName" id="someId" required="required" pattern="(([1-9]?[0-9]?[5-9]?)|([0-9]?[5-9]?)|([5-9]?))"/>

http://jsfiddle.net/hiperportal/Qa5xS/2/

However it is not working, only works if typed 04, and not only 4.

Note: minimum number 5, maximum number 999.

Thank you.

1 answer

2


That will probably solve.

 <input type="text" name="someName" id="someId" required="required" pattern="((^[5-9]$)|(^[1-9][0-9]?[0-9]$))"/>

Test HTML in jsfiddle.net

Explanation of the expression:

((^[5-9]$)|(^[1-9][0-9]?[0-9]$))
  • ^ top of the list
  • [5-9] take any value from 5 to 9
  • $ end of the list
  • | "or". This allows searching in the next regex if the first fails
  • ^[1-9][0-9] This takes any value from 10 to 99 (*)
  • [0-9]$ = optional ai takes anything from 1 to 9 that is the last character in the list.

(*) - the ? means optional. When so placed within ^[1-9][0-9]?[0-9]$ means that the verification of the medium is optional, which allows numbers between 10 and 99.

  • Thank you, it worked perfectly. I thank you for your explanations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.