Javascript can only enter regular expressions such as numbers, numbers, and letters.

Source: Internet
Author: User

1. Only numeric code can be entered in the text box (decimal point cannot be entered)
<Input onkeyup = "this. value = This. value. replace (/\ D/g, '')" onafterpaste = "this. value = This. value. replace (/\ D/g, '')">

2. Only numbers can be entered, and decimal points can be entered.
<Input onkeyup = "If (isnan (value) Execcommand ('undo ')" onafterpaste = "If (isnan (value) Execcommand ('undo')">
<Input name = txt1 onchange = "If (/\ D /. test (this. value) {alert ('only numbers allowed '); this. value = '';}">

3. Number and decimal point method 2
<Input type = text t_value = "" o_value = "" onkeypress = "If (! This. value. Match (/^ [\ + \-]? \ D *? \.? \ D *? $/) This. value = This. t_value; else this. t_value = This. value; If (this. value. Match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )?)? $/) This. o_value = This. Value "onkeyup =" If (! This. value. Match (/^ [\ + \-]? \ D *? \.? \ D *? $/) This. value = This. t_value; else
This. t_value = This. value; If (this. value. Match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )?)? $/) This. o_value = This. Value "onblur =" If (! This. value. Match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )? | \. \ D *?)? $/) This. value = This. o_value; else {If (this. value. match (/^ \. \ D + $/) This. value = 0 + this. value; If (this. value. match (/^ \. $/) This. value = 0; this. o_value = This. value} ">

4. Only letters and Chinese characters can be entered
<Input onkeyup = "value = value. replace (/[\ D]/g, '')" onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[\ D]/g, '')" maxlength = 10 name = "Numbers">

5. Only English letters and numbers are allowed. Chinese characters are not allowed.
<Input onkeyup = "value = value. Replace (/[^ \ W \. \/]/ig,'') ">

6. Only numbers and English letters can be entered. <font color = "red"> Chun </font>
<Input onkeyup = "value = value. Replace (/[^ \ d | Chun]/g,'') ">

7. After the decimal point, you can enter a maximum of two digits (numbers and Chinese characters). You cannot enter letters or operator numbers:
<Input onkeypress = "If (event. keycode <48 | event. keycode> 57) & event. keycode! = 46 |/\. \ D $/. Test (value) event. returnvalue = false ">

8. A maximum of two digits (numbers, letters, and Chinese characters) can be entered after the decimal point. You can enter the operator number:
<Input onkeyup = "this. value = This. value. replace (/^ (\-) * (\ D + )\. (\ D ). * $/, '$1 $2. $ 3') ">

It can only be a number, a decimal point, or a plus or minus sign.

Onkeypress = "Return event. keycode> = 4 & event. keycode <= 57"

 

========================================================== ========================================================== ==================================

JS allows only numbers in the input box
Description
1 can only input 0 to 9 and decimal point
2. Only numbers can be pasted.
3. content cannot be dragged in.
4 forbidden to use input methods

<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> javascript can only enter numbers and decimal places </title>
<Script language = "JavaScript" type = "text/JavaScript">
Function clearnonum (OBJ)
{
// Replace all non-numeric values first, except numbers and.
OBJ. value = obj. value. Replace (/[^ \ D.]/g ,"");
// Ensure that the first digit is a number instead of a number.
OBJ. value = obj. value. Replace (/^ \./g ,"");
// Ensure that there is only one and no more.
OBJ. value = obj. value. Replace (/\. {2,}/g ,".");
// Ensure that the message appears only once, but not more than twice.
OBJ. value = obj. value. replace (". "," $ # $ "). replace (/\. /g ,""). replace ("$ # $ ",". ");
}
</SCRIPT>
</Head>
<Body>
<! -- Add the following code to the <body> and </body> -->
Only text boxes with numbers and decimal places can be entered: <input id = "input1" onkeyup = "clearnonum (this)">
</Body>
</Html>

 

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> only numbers can be entered in the input box </title>
</Head>

<Body>
<Input type = "text" name = "num" id = "num" onkeypress = "Return keypress (event)">
<SCRIPT type = "text/JavaScript">
Function keypress (e ){
EE = e? E: window. event? Event: NULL;
VaR keynum = event. keycode;
VaR keychar = string. fromcharcode (keynum );
VaR regdig =/\ D/g;
If (regdig. Test (keychar) | keychar = ""){
Return true;
}
VaR regdot =/\./g;
If (regdot. Test (keychar )&&! Regdot. Test (document. getelementbyid ("num"). Value )){
Return true;
}
Return false;
}
</SCRIPT>
</Body>
</Html>

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.