Many people have written this thing, but I cannot find the one that meets the requirements when I use it temporarily today. So I will write it immediately, now that you have finished writing and are fully qualified to meet your needs, let's take a look.
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 thisCopy codeThe Code is as follows: <input type = "text" style = "ime-mode: disabled" onkeyup = "return ValidateNumber ($ (this), value)"/>
• 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 verificationCopy codeThe 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.Copy codeThe 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 changedCopy codeThe Code is as follows: <input type = "text" style = "ime-mode: disabled" onkeyup = "return ValidateNumber (this, value)"/>
Only numbers can be entered (javascript only)Copy codeThe 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)Copy codeThe 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.Copy codeThe 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.Copy codeThe 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;