The input box only supports Chinese characters, numbers, and integers.

Source: Internet
Author: User

Use JS to restrict the input box to only Chinese characters, numbers, integers, and other effects. There are multiple types of code, which may help you and focus more on helping customers.

First, only numbers and decimal points are allowed.
<Input onKeypress = "return (/[\ d.]/. test (String. fromCharCode (event. keyCode)" style = "ime-mode: Disabled">

Second, the judgment is more detailed, and even 22 .. 2 is not counted as a number.

<Script>
Function check (){
If (isNaN (tt. value ))
{Alert ("invalid character! ");
Tt. value = "";}
}
</Script>
<Input type = "text" name = "tt" onkeyup = "check ();">

Third, only integers are allowed. In fact, some restrictions can be made based on article 3.

<Script language = javascript>
Function onlyNum ()
{
If (! (Event. keyCode = 46 )&&! (Event. keyCode = 8 )&&! (Event. keyCode = 37 )&&! (Event. keyCode = 39 ))
If (! (Event. keyCode> = 48 & event. keyCode <= 57) | (event. keyCode> = 96 & event. keyCode <= 105 )))
Event. returnValue = false;
}
</Script> <input onkeydown = "onlyNum ();" style = "ime-mode: Disabled>
Conclusion: In fact

Style = "ime-mode: Disabled
This sentence is more practical. To disable the input method. It saves some people the trouble of entering numbers in full angles, and the result cannot be entered to find you crying. It is also a blame for your poor design.

Only numbers are allowed.
<Input name = "username" type = "text" onkeyup = "value = this. value. replace (/\ D +/g,'') ">

Only English letters, numbers, and underscores (_) are allowed)
<Input name = "userna 1. Only numbers and decimal points are allowed.
<Input onKeypress = "return (/[\ d.]/. test (String. fromCharCode (event. keyCode)" style = "ime-mode: Disabled">

Second, the judgment is more detailed, and even 22 .. 2 is not counted as a number.

<Script>
Function check (){
If (isNaN (tt. value ))
{Alert ("invalid character! ");
Tt. value = "";}
}
</Script>
<Input type = "text" name = "tt" onkeyup = "check ();">

Third, only integers are allowed. In fact, some restrictions can be made based on article 3.

<Script language = javascript>
Function onlyNum ()
{
If (! (Event. keyCode = 46 )&&! (Event. keyCode = 8 )&&! (Event. keyCode = 37 )&&! (Event. keyCode = 39 ))
If (! (Event. keyCode> = 48 & event. keyCode <= 57) | (event. keyCode> = 96 & event. keyCode <= 105 )))
Event. returnValue = false;
}
</Script> <input onkeydown = "onlyNum ();" style = "ime-mode: Disabled>
Conclusion: In fact

Style = "ime-mode: Disabled
This sentence is more practical. To disable the input method. It saves some people the trouble of entering numbers in full angles, and the result cannot be entered to find you crying. It is also a blame for your poor design.

Only numbers are allowed.
<Input name = "username" type = "text" onkeyup = "value = this. value. replace (/\ D +/g,'') ">

Only English letters, numbers, and underscores (_) are allowed)
<Input name = "username" type = "text" style = "ime-mode: disabled">
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^ \ w \. \/]/ig,'') ">

Only English letters, numbers, and @ can be entered @
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^ \ w = @ &] | _/ig,'') ">

Only Chinese characters are allowed.
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g,'') ">
Me "type =" text "style =" ime-mode: disabled ">
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^ \ w \. \/]/ig,'') ">

Only English letters, numbers, and @ can be entered @
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^ \ w = @ &] | _/ig,'') ">

Only Chinese characters are allowed.
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g,'') ">

Related Article

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.