The text box only agrees to enter numbers

Source: Internet
Author: User
The text box only agrees to enter numbers. Net/JavaScript

<Input type = "text" name = "test" onkeyup = "test1.value = (this. value = This. value. replace (/\ D/g ,''). substring (0, 6 )). substring (0, 3) "> <br/>
<Input name = "test1" type = "text">

There is a difference between up and down.

Only numbers, letters, and underscores can be entered.

<Input id = "txtshopnumber" runat = "server" class = "input_text" maxlength = "8" onkeyup = "value = value. replace (/[^ (\-) \ W \. \/]/ig, '')"/>

The regular expression can only enter numbers: "^ [0-9] * $"
Only n digits can be entered: "^ \ D {n} $"
You can only enter at least N digits: "^ \ D {n,} $"
You can only enter M-N digits: "^ \ D {m, n} $"
Only numbers starting with zero and non-zero can be entered: "^ (0 | [1-9] [0-9] *) $"
You can only enter a positive number with two decimal places: "^ [0-9] + (. [0-9] {2 })? $"
Only a positive number with 1-3 decimal places can be entered: "^ [0-9] + (. [0-9] {1, 3 })? $"
Only a non-zero positive integer can be entered: "^ \ +? [1-9] [0-9] * $"
Only a non-zero negative integer can be entered: "^ \-[1-9] [0-9] * $"
Only 3 characters can be entered: "^. {3} $"
Only a string consisting of 26 English letters can be entered: "^ [A-Za-Z] + $"
Only a string consisting of 26 uppercase letters can be entered: "^ [A-Z] + $"
Only a string consisting of 26 lower-case English letters can be entered: "^ [A-Z] + $"
Enter only a string consisting of a number and 26 English letters: "^ [A-Za-z0-9] + $"
Only a string consisting of digits, 26 English letters, or underscores can be entered: "^ \ W + $"
Verify the User Password: "^ [A-Za-Z] \ W {5, 17} $". The correct format is: starts with a letter and ranges from 6 to 18,

It can only contain characters, numbers, and underscores.
Check whether ^ % & ',; =? $ \ "And other characters:" [^ % & ',; =? $ \ X22] +"
Only Chinese characters can be entered: "^ [\ u4e00-\ u9fa5], {0,} $"
Verify email address: "^ \ W + [-+.] \ W +) * @ \ W + ([-.] \ W + )*\. \ W + ([-.] \ W +) * $"
Verify interneturl: "^ http: // ([\ W-] + \.) + [\ W-] + (/[\ W -./? % & =] *)? $"
Verification phone number: "^ (\ D {3, 4} \) | \ D {3, 4 }-)? \ D {7, 8} $"

Correct format: XXXX-XXXXXXX, XXXX-XXXXXXXX, XXX-XXXXXXX ",

XXX-XXXXXXXX, xxxxxxx, XXXXXXXX ".
Verify the ID card number (15 or 18 digits): "^ \ D {15} | \ D {} 18 $"
12 months of verification: "^ (0? [1-9] | 1 [0-2]) $ "the correct format is:" 01 "-" 09 "and" 1 "" 12"
31 days of verification for a month: "^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $"

The correct format is "01", "09", and "1", "31 ".


You can only enter numbers, decimal places, English letters, Chinese characters, and other types of codes in the text box using the regular expression in the regular form.


1. The text box can only enter the number code (not the decimal point)
<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 can be entered '); 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, and 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. Only two digits (numbers and Chinese characters) can be entered after the decimal point, and letters and operator numbers cannot be entered:
<Input onkeypress = "If (event. keycode <48 | event. keycode> 57) & event. keycode! = 46 |/\. \ D $/. Test (value) event. returnvalue = false ">

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

The text box only agrees to enter numbers

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.