Home > Web Front-end > JS Tutorial > 'JavaScript' restricts Input to only input numbers. Implementation ideas and codes_javascript skills

'JavaScript' restricts Input to only input numbers. Implementation ideas and codes_javascript skills

WBOY
Release: 2016-05-16 17:35:52
Original
1006 people have browsed it

Many people have written about this thing, but when I wanted to use it temporarily today, I couldn't find one that met the needs, so I wrote one immediately. Now that it's all written and it meets the needs, I put it up for everyone to give it a try.

The requirement is very simple. A text box must be restricted to numbers (or decimal points) and must support IE and Firefox.
HTML Input is like this

Copy code The code is as follows:

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

•Which Style is in IE browser If enabled, the user cannot use the input method in this text box.
•Onkeyup is js written by ourselves. The complete code is below.
-------------------------------------------------- ----------------------------------
Only numbers can be entered for verification
Copy code 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 those that are not numbers.
-------------------------------------------------- ----------------------------------
In practice, what often needs to be verified is the field with a decimal point. A bunch of weird values ​​that I found on the road could be entered as "1.2.3456". In fact, it can be verified by just changing the Regex a little.
Copy code 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 is for friends in need.
Because some netizens said that this kind of thing does not need to use jQuery. Indeed, it does not need to be used, so let’s write a pure javascript version
HTML needs to be changed
Copy code The code is as follows:



Only numbers can be entered (pure javascript)
Copy code The code is as follows:

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

Decimals can be entered (pure javascript)
Copy Code 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 said that they want to limit it to only one decimal place, so let’s make some small changes. In fact, the only important thing is to change RegExp
Copy the code 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 are using the evil IE, you will get an annoying null when you enter a non-number at the beginning, so you have to rewrite it to filter it out like this
Copy code 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 labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template