Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop IE11 removing text from input type number

In Internet Explorer 11, if you have the following:

<input type="number" min="1" max="12" pattern="[0-9]*" >

When a user tries to input letters rather than numbers, the browser just clears the input. This behaviour is undesirable as I want our validation to handle this, rather than the browser automatically doing it for me.

Does anybody know how to change this behaviour?

Example Image

I'm using the number input type for validation and also to get the correct keyboard in iOS.

JSFiddle demo.

like image 673
hcharge Avatar asked Oct 08 '14 10:10

hcharge


People also ask

How to remove the Clear button from the input element?

The clear button is only shown on focused, non-empty text <input> elements. To solve this issue you can remove this cross button or icon use ::-ms-clear CSS property. A passionate blogger by heart and mind, I have been working in this field for 7 years now.

How to clear search inputs in Internet Explorer and Edge browser?

In Internet Explorer and Edge browser, input type search fields shows a cross icon at top right side that is actually a clear button. When user clicks on that cross (X) icon, it clear the search input. It’s a useful feature, but when you want same styling consistency across all the browser it becomes an issue or bug.

How to stop my computer from forward deleting text as I type?

[Solution] How to Stop My Computer from Forward Deleting Text as I Type How to Stop Your PC from Forward Deleting Text as You Type - Toggling Off Overtype Mode with the Insert Key 1 Press Insert or Ins once. 2 Press Ctrl+Z to restore accidentally-deleted text. 3 Type your text again. See More....

How to hide spin arrow from input type number in HTML5?

In < input type="number" > field by default the up and down arrows are appear on the right side of the input box, these are called spinners. Hide the spin arrow from input type number. These spinners can be easily hide using CSS properties. The input type is a form element that is new in HTML5.


1 Answers

Other browsers may keep this text present, however entering "aaa" into an input element whose type attribute is set to "number" will mean that your element's value property is empty, so your validation method would most likely regard this as a lack of content rather than a non-numerical value anyway.

<input type="number" value="aaa" />
console.log(document.querySelector('input').value);
> ""

I've altered your JSFiddle demo to reflect this: http://jsfiddle.net/e1132tg5/3/.

If you need to accept non-numerical data in your input element then the solution is quite simply to change your element's type to "text" instead of "number" and rely solely on your own JavaScript validation methods for handling the validation (as it appears you want anyway).

<input type="text" >

You can always also use your JavaScript to determine whether the user is viewing your site on a mobile device and change the type attribute dynamically.

An alternate solution would be to inform the user that the field must be numerical if the element is blurred with no text present (after IE's automatic removal), but this would be better discussed on SE's UserExperience site.

like image 79
James Donnelly Avatar answered Sep 22 '22 06:09

James Donnelly