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

Source: Internet
Author: User

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

 

Use JavaScript to restrict the input box to only Chinese characters, numbers, integers, and other effects. You can use multiple types of code. I hope it will be helpful to you and pay more attention to iang.com.

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,'') ">

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.