"JavaScript" limits Input to numbers only. Implementation ideas and code _ javascript skills

Source: Internet
Author: User
A text box must be limited to numbers (or decimal places) and support IE and Firefox. Next we will introduce how to solve this problem, however, I cannot find the one that meets the requirements when I use it temporarily today, so I will write it immediately. Now that it's all done and it's enough to meet the requirements, let's give it a whip.

The requirement is simple. Only numbers (or decimal points) can be entered in a text box and IE and Firefox are supported.
HTML Input is like this

The Code is as follows:




• The Style is valid in IE browser, so that users cannot use the input method in this text box.
• Onkeyup is the js we have written. The complete Code is as follows.
--------------------------------------------------------------------------------
Only numbers can be entered for verification

The Code is as follows:


Function ValidateNumber (e, pnumber ){
If (! /^ \ D + $/. test (pnumber )){
$ (E). val (/^ \ d +/. exec ($ (e). val ()));
}
Return false;
}


The demo uses Regex to filter out non-numbers.
--------------------------------------------------------------------------------
In practice, it is often necessary to verify that there are decimal places in the column. A bunch of records found on the Internet are strange values that can be entered "1.2.3456, in fact, you only need to change the Regex to verify it.

The Code is as follows:


Function ValidateFloat (e, pnumber ){
If (! /^ \ D + [.]? \ D * $/. test (pnumber )){
$ (E). val (/^ \ d + [.]? \ D */. exec ($ (e). val ()));
}
Return false;
}


In this way, you can enter an integer or a decimal point.
--------------------------------------------------------------------------------
The above will be used by friends in need.
Some netizens say that this kind of stuff does not need to be used in jQuery. It does not need to be used, so I will write a pure javascript version.
HTML needs to be changed

The Code is as follows:




Only numbers can be entered (javascript only)

The Code is as follows:


Function ValidateNumber (e, pnumber ){
If (! /^ \ D + $/. test (pnumber )){
E. value =/^ \ d +/. exec (e. value );}
Return false;
}


You can enter decimals (javascript only)

The Code is as follows:


Function ValidateNumber (e, pnumber)
{
If (! /^ \ D + [.]? \ D * $/. test (pnumber ))
{
E. value =/^ \ d + [.]? \ D */. exec (e. value );
}
Return false;
}


Some netizens say that he wants to limit only one digit after the decimal point, so let's make a small change. In fact, the focus is only to change RegExp.

The Code is as follows:


Function ValidateFloat2 (e, pnumber)
{
If (! /^ \ D + [.]? [1-9]? $/. Test (pnumber ))
{
E. value =/\ d + [.]? [1-9]? /. Exec (e. value );
}
Return false;
}


If you use Internet Explorer, then when you enter a non-number at the beginning, it will give you a nasty null, so you have to rewrite it to filter it out.

The Code is 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.