Js control input box can only enter one digit and is a number tutorial

Source: Internet
Author: User

If you encounter such a problem in a project, you must enter only one digit in the input box and enter the following code after reading the information:

<Div class = "main-out">
<P class = "identifying-title"> enter the verification code provided by the enterprise </p>
<Div class = "pass-box">
<! -- Black horizontal box -->
<Div class = "pass-line">
<Div class = "line-box"> <span class = "line-one"> </span> </div>
<Div class = "line-box"> <span class = "line-two"> </span> </div>
<Div class = "line-box"> <span class = "line-three"> </span> </div>
<Div class = "line-box"> <span class = "line-four"> </span> </div>
<Div class = "line-box"> <span class = "line-five"> </span> </div>
<Div class = "line-box"> <span class = "line-six"> </span> </div>
</Div>
<! -- Enter the verification code box -->
<Div class = "passInput" id = "on">
<Input type = "text" class = "inputCont-one" maxlength = "1"/>
<Input type = "text" class = "inputCont-two" maxlength = "1"/>
<Input type = "text" class = "inputCont-three" maxlength = "1"/>
<Input type = "text" class = "inputCont-four" maxlength = "1"/>
<Input type = "text" class = "inputCont-five" maxlength = "1"/>
<Input type = "text" class = "inputCont-six" maxlength = "1"/>
<! -- <Input type = "text" onkeyup = "this. value = this. value. replace (/\ D/g, '')" onafterpaste = "this. value = this. value. replace (/\ D/g, '')" maxlength = 1> -->
</Div>
</Div>
</Div>

$ (". InputCont-one"). focus ();
$ (". Line-one"). hide ()
Onload = function (){
Var txts = on. getElementsByTagName ("input ");
For (var I = 0; I <txts. length; I ++ ){
Var t = txts [I];
T. index = I;
T. setAttribute ("readonly", true );
T. onkeyup = function (){
If (this. value = this. value. replace (/\ D/g ,'')){
Var next = this. index + 1;
If (next> txts. length-1) return;
Txts [next]. removeAttribute ("readonly ");
Txts [next]. focus ();
$ (". Line-box"). eq (next). find (". line"). hide ();
} Else {
$ (This). focus ();
}
}
}
Txts [0]. removeAttribute ("readonly ");
}
 
& Nbsp;

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.