Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Input type=number removes leading zero

In Chrome 15, when using the element as a text field, leading zeros (e.g. 011) are removed even if the number entered does not break the validation rules (e.g. min, max). Is there an attribute to force the zero to remain in the field after it loses focus? The application for this is numeric data such as international phone prefixes.

like image 994
Evan Avatar asked Dec 08 '11 20:12

Evan


People also ask

How do you prevent a zero as the first character when typing in input type number?

keypress(function(evt) { if (evt. which == "0".

Which input type in html5 can contain only a numeric value?

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.

How do you restrict inputs to only numbers?

1. Using <input type="number"> The standard solution to restrict a user to enter only numeric values is to use <input> elements of type number. It has built-in validation to reject non-numerical values.


2 Answers

<input type="text" pattern="[0-9]*" ...

that should do it for you. Will bring up the numeric keypad on iPhone and the nicer Android phones I've tested on.

like image 54
Lar Avatar answered Oct 06 '22 05:10

Lar


<input type="tel"> has been introduced for this exact purpose. It's one of the new input types in HTML5.

like image 32
Dennis Traub Avatar answered Oct 06 '22 04:10

Dennis Traub