"JavaScript" limit input can only input digital implementation ideas and code _javascript skills

Source: Internet
Author: User
This thing many people wrote, but today temporarily need to use when can't find meet the demand, so immediately to write a, since all finished and also full meet the needs of the put up to let everyone whip a whip.

The requirement is simple, a text box must limit only the number (or decimal point) and support IE and Firefox.
HTML Input is like this drop
Copy Code code as follows:

<input type= "text" style= "ime-mode:disabled" onkeyup= "Return ValidateNumber ($ (this), value)"/>

One of the Style is effective in IE browser, so that users can not use the input method in this text box.
* and onkeyup is our own writing JS, complete Code in the following.
--------------------------------------------------------------------------------
Verify that only numbers are entered
Copy Code code as follows:

function ValidateNumber (e, pnumber) {
if (!/^\d+$/.test (Pnumber)) {
$ (E). Val (/^\d+/.exec ($ (e). Val ()));
}
return false;
}

The demo uses a Regex to filter out the numbers that are not part of it.
--------------------------------------------------------------------------------
In practice, it is often necessary to verify that there is a decimal point of the field, the network to find a bunch of can be input "1.2.3456" this strange value, in fact, as long as a small change to the Regex can be verified.
Copy Code code as follows:

function Validatefloat (e, pnumber) {
if (!/^\d+[.]? \d*$/.test (Pnumber)) {
$ (E). Val (/^\d+[.]? \d*/.exec ($ (e). Val ()));
}
return false;
}

This way you can enter an integer or you can enter a decimal point
--------------------------------------------------------------------------------
The above is for friends who need to use it.
Because a netizen says this kind of thing does not need to use to jQuery indeed, is not to use so to write a version of pure JavaScript
HTML has to change
Copy Code code as follows:

<input type= "text" style= "ime-mode:disabled" onkeyup= "return ValidateNumber (this,value)"/>

Only digits can be entered (pure JavaScript)
Copy Code code as follows:

function ValidateNumber (e, pnumber) {
if (!/^\d+$/.test (Pnumber)) {
E.value =/^\d+/.exec (E.value);}
return false;
}

You can enter decimal (pure JavaScript)
Copy Code code as follows:

function ValidateNumber (e, Pnumber)
{
if (!/^\d+[.]? \d*$/.test (Pnumber))
{
E.value =/^\d+[.]? \d*/.exec (E.value);
}
return false;
}

Some netizens said he wanted to limit only a decimal point after a good, so the small change in fact, the focus is only regexp to change it
Copy Code code as follows:

function ValidateFloat2 (e, Pnumber)
{
if (!/^\d+[.]? [1-9]?$/.test (Pnumber))
{
E.value =/\d+[.]? [1-9]?/.exec (E.value);
}
return false;
}

If you are using all the evils of IE, when you first enter the Non-numeric, you will send you a nasty null and then rewrite it to filter out
Copy Code code as follows:

if (!/^\d+$/.test (pnumber))
{
var newvalue =/^\d+/.exec (E.value);
if (newvalue!= null)
{
E.value = newvalue;
}
Else
{
E.value = "";
}
}
return false;
Related Article

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.