JavaScript regular expression control input can only enter a number

Source: Internet
Author: User

Cannot enter Chinese
<input type= "text" name= "TextField" onkeyup= "This.value=this.value.replace (/[^/da-z_]/ig, ');" />
Only numbers and underscores can be entered
<input onkeypress= "Return (/[/d_]/.test (String.fromCharCode (Event.keycode))" style= "ime-mode:disabled"/>
Only numbers and decimal points can be entered
<input onkeypress= "return (/[/d.) /.test (String.fromCharCode (Event.keycode))) "style=" ime-mode:disabled "/>

Allow only Chinese characters to be entered </br>
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/u4e00-/u9fa5]/g, ')" >


One is to allow only numbers and decimal points to be entered.
<input onkeypress= "return (/[/d.) /.test (String.fromCharCode (Event.keycode))) "style=" ime-mode:disabled ">

Second, the judgment is more detailed, even 22. 2 That's not a number.

<script>
function Check () {
if (IsNaN (Tt.value))
{alert ("illegal character! ");
Tt.value= "";}
}
</script>
<input type= "text" Name= "tt" onkeyup= "check ();" >

The third, only allows the input of integers. In fact, we can also make some restrictions according to the third article extrapolate.

<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>
The conclusion, in fact

Style= "ime-mode:disabled
This sentence is more practical. Turn off the IME. Save some people to open the full-width input number, the result input does not come in to find you kutianmalei, also blame you design bad.

Allow only numeric input
<input name= "username" type= "text" onkeyup= "Value=this.value.replace (//d+/g, ')" >

Value=value.replace (/[^\d\.) /g, ");

Only English letters, numbers, and underscores are allowed (the following two methods are implemented)
<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 &[email are allowed protected]
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/[email protected]&]|_/ig, ')" >

Allow only Chinese characters to be entered
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/u4e00-/u9fa5]/g, ')" >

JavaScript regular expression control input can only enter a number

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.