Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 input number min max not working with required

Tags:

html

css

I would like to force the user to type their age. I also would like to make sure they only enter between 18-99. Is that possible with HTML5 attributes like required, pattern, min and max? Seems like it is not working

<form>
    <input type="number" name="Q2age" id="Q2age" size="10" min="18" max="99" pattern="[1-8][0-9]" required>
    <button type="submit" class="button" id="test">Submit</button>
</form>

Am I doing something wrong here? I use Firefox 22.0 on Ubuntu 12.0.4LTS (tested on Chrome as well, but doesn't work.) Thank you for your help.

like image 317
user1330974 Avatar asked Jul 26 '13 04:07

user1330974


People also ask

How can I limit possible inputs in a html5 number element?

You can specify the min and max attributes, which will allow input only within a specific range. This only works for the spinner control buttons, however. Although the user may be able to type a number greater than the allowed max , the form will not submit.

How do you set the minimum and maximum value of an input type number?

The min attribute specifies the minimum value for an <input> element. Tip: Use the min attribute together with the max attribute to create a range of legal values. Note: The max and min attributes works with the following input types: number, range, date, datetime-local, month, time and week.


2 Answers

This is the expected behaviour because FF21 doesnt supports min,max attribute...

you can check it in http://html5test.com/

Check the screenshot enter image description here

like image 163
Arun Bertil Avatar answered Sep 29 '22 13:09

Arun Bertil


Your code works fine. Check this fiddle (I'm using latest chrome)

My only concern you is your pattern="[1-8][0-9]" can simple use this

pattern="[0-9]{2}"
like image 38
Praveen Avatar answered Sep 29 '22 11:09

Praveen