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;