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;