Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify the exact number of digits that an user have to insert into an input field?

I am pretty new in HTML5 and I have the following problem. Into a page I have an input tag like this:

<input id="codiceFiscaleEnte" class="form-control" name="numeroProtocollo" type="number" th:value="*{codiceFiscaleEnte}" required="required"></input>

and I have to do some validation on it.

So I know that the required="required" attribute means that the user have to insert a value for this field and that the type="number" specify that this value have to reprsent a number.

Now my problem is: can I specify in some way that this number have to be composed by exactly 11 digits? How can I do it using HTML5 attributes?

like image 449
AndreaNobili Avatar asked Feb 11 '16 15:02

AndreaNobili


People also ask

How do I limit the number of characters in an input field in HTML?

The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

How do you make an input field only take numbers?

By default, HTML 5 input field has attribute type=”number” that is used to get input in numeric format. Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the input field that will popup numeric keyboard on mobile devices.

What input type tags is used to define a numeric input field?

The HTML input tag is used for describing the range of numbers in HTML form. The <input> tag is used to define an input field where users enter data.


1 Answers

Use pattern:

<input type="text" name="numeroProtocollo" required pattern="[0-9]{11}">

It allows only numbers with a minimum and maximum length of 11

Test it:

https://jsfiddle.net/oegjdszx/1/

like image 161
Marcos Pérez Gude Avatar answered Nov 13 '22 21:11

Marcos Pérez Gude