HTML5 Input[number] Type input non-numeric character Val () to "" and solution

Source: Internet
Author: User

HTML5 the new number input type will call up the numeric keypad when the input box has the focus, increasing the effect of the experience. However, on some Android machines, you can switch the word input, and users will accidentally enter non-numeric characters such as "+" and "-". After testing, the number type automatically ignores alphabetic characters and other non-numeric characters, except for "+" "-" ". These two characters can be entered, because these two are symbols of positive and negative numbers and decimal points.

The number type also has a pit point where the value of the corresponding DOM element is null ("") if the contents of the input box are not pure digits (positive, negative numbers are OK), and the contents contain some characters.

So either the native document.getElementById ("TXT"). Value or JQ's $ (' selector '). Val () is empty.

This problem is difficult to solve, even the KeyUp and keypress listening input boxes will be met.

My requirement is that you cannot enter a decimal, that is, you cannot enter a character other than a number. The character is deleted after the input.

My practice is to use regular expressions to detect input characters and remove the last character. Monitor with KeyUp.

Keyup/keypress monitoring is not perceptible at the moment of entering a decimal point or +--the value of the DOM does not change. You can tell by value when you enter a numeric character after the symbol.

$ ("#moneyInput"). On ("KeyUp", function (e) {
var obj = $ ("#moneyInput"),
Value = Obj.val ();
if (! ( /(^\+? [1-9] [0-9]*$)/.test (value)) {
Value = value.substring (0,value.length-1);
Obj.val ("");
Obj.val (value);
}
});

HTML5 Input[number] Type input non-numeric character Val () to "" and solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.