1

My question is about validation in html. At the moment, if I input 100 or 1000 it will work as intended.

This is my html code:

<tr>
    <th scope="row" colspan = "1"><font size="5">Number of Guests:</th>
    <td colspan = "2"><input type = "text" name = "guest" maxlength = "4" id="guest" 
    pattern="[0-9]{3,4}" title = "Number of guests must be more than 100 people."></td>
</tr>

My problem is that when I try to input 001 or 000 it still works. How can I counter that problem? Do I have to validate in php? Thank you for any comments or suggestions.

6
  • 1
    Please Clarify what exactly do you want? Commented Oct 12, 2015 at 7:09
  • your saying value form 100 to 1000? is that then <input type = "number" name = "guest" min = "100" max="1000" id="guest" title = "Number of guests must be more than 100 people."> Commented Oct 12, 2015 at 7:13
  • i want only value from 100-1000, i want the validation to reject if i input 001 or 000 or any starting with 0 Commented Oct 12, 2015 at 7:15
  • if you want to remove arrows at right side of input type field use this CSS input[type=number]::-webkit-inner-spin-button, input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; -moz-appearance: none; appearance: none; margin: 0; } Commented Oct 12, 2015 at 7:21
  • In order to answer also at the second part of your question, YES, you should validate input from user server-side anyway because HTML5 input pattern attribute and input type=number do not present the same behavior in all browsers: pls see caniuse.com pattern and caniuse.com number Commented Oct 12, 2015 at 8:05

4 Answers 4

3

Try This it will work for numbers

<input type = "number" name = "guest" min = "100" max="1000" id="guest" title = "Number of guests must be more than 100 people.">
Sign up to request clarification or add additional context in comments.

1 Comment

+1 It's enjoyable when it doesn't require a regex to work. One should almost never use a regex unless it's the only viable solution as it contributes to make the code harder to understand. P.S.: I'd also answer his subquestion about php, it's quite straightforward IMO, the OP is probably just unaware of "server-side first" rule of thumb.
1

Using Kóta Péter idea

pattern="^[1-9][0-9]{2,3}"

Comments

0

Try this pattern: ^(0?[1-9][0-9]{2}|[1-9][0-9]{3})$

this way you disallow input 0100,001,099,0099

Comments

0

Use following code to get you desired value only.

   <input type = "text" name = "guest" maxlength = "4" id="guest" min="100" max="1000" pattern="^[1-9][0-9]*$" title = "Number of guests must be more than 100 people.">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.