JS through the regular limit input input box can only enter an integer, decimal (Amount or cash) two-bit decimal _ Regular expression

Source: Internet
Author: User
Tags regular expression

First: limits can only be integers

<input type = "text" name= "number" id = ' number ' onkeyup= ' if (! /^d+$/.test (This.value)) {alert (' only Integer '); this.value= ';} '/>

Direct alert if it's not an integer

Second: The limit is two decimal digits

<input type = "text" name= "price" id = ' price ' onkeyup= ' if (! /^d* (?:. d{0,2})? $/.test (This.value)) {alert (' Enter only digits, only two digits after the decimal point '); this.value= ';} '/>

Principle:

Execution alert is not satisfied by the regular expression judgment.

The first regular expression is the/^d+$/representation can be one or more digits

The second regular expression is

/^d* (?:. d{0,2})? $/

The expression must be a number at the beginning and the end of the number.

Here the emphasis is to the end of numbers, which are usually decimal 1 in the computer. , 2. This type of writing, but the end of the decimal point, is correct. Here forces the number to end.

Test ()

Just find the part that satisfies you and return to true.

It means yes.

/d/. Test (' a ')//False
/d/. Test (' a ')//True
/d/. Test (' a ')/true

So make sure who starts and who ends. Start with $, end with ^

Here is a brief introduction to JS regular limit input box code

1. Only numbers and English can be entered:

<input onkeyup= "Value=value.replace (/[\w]/g,") "Onbeforepaste= 
" clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') " 

2. Only numbers can be entered:

<input onkeyup= "Value=value.replace (/[^\d]/g,") "Onbeforepaste= 
" clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\d]/g, ') "id=" Text2 "name=" Text2 " 
>

3. Only the full angle can be entered:

<input onkeyup= "Value=value.replace (/[^\uff00-\uffff]/g,") "Onbeforepaste= 
" clipboarddata.setdata (' text ' , Clipboarddata.getdata (' text '). Replace (/[^\uff00-\uffff]/g, ') " 

4. Only Chinese characters are entered:

<input onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g,") "Onbeforepaste= 
" clipboarddata.setdata (' text ' , Clipboarddata.getdata (' text '). Replace (/[^\u4e00-\u9fa5]/g, ') " 

5. Email Address verification:

var Regu = 
"^" ([0-9a-za-z]+) | ( [0-9a-za-z]+[_.0-9a-za-z-]*[0-9a-za-z]+)] @ ([a-za-z0-9-]+[.]) + ([a-za-z]{2}|net|net|com|com|gov|gov|mil|mil|org|org|edu|edu|int|int) $ " 
var re = new RegExp (regu); 
if (S.search (re)!=-1) {return 
true; 
} else { 
window.alert ("Please enter a valid e-mail address!"). ") return 
false; 
}

6. ID Card:

"^\\d{17} (\\d|x) $" 
7.17 Regular Expression 
"^\\d+$"//non-negative Integer (positive integer + 0) 
"^[0-9]*[1-9][0-9]*$"//Positive integer 
^ (-\\d+) | ( 0+)) $ "///Non positive integer (negative integer + 0) 
" ^-[0-9]*[1-9][0-9]*$ "//Negative integer" 
^-?\\d+$ "//Integer 
" ^\\d+ (\\.\\d+)? $ "//nonnegative floating-point number (positive floating-point number + 0) 
"^ ([0-9]+\\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $ "//Positive floating-point number 
" ^ (-\\d+ (\\.\\d+)?) | (0+ (\\.0+)) $ "//non-positive floating-point number (negative floating-point number + 0) 
" ^ (-([0-9]+\\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $ "//negative floating-point number 
" ^ (-?\\d+) (\\.\\d+) $ "//floating-point number 
" ^[a-za-z]+$ "/////26 alphanumeric string 
" ^[a-z]+$ "// A string "^[a-z]+$" consisting of 26 uppercase letters "/////" ^[a-za-z0-9]+$ "/////" ^\\w+$ "//////// 
A string of numbers, 26 English letters, or underscores 
"^[\\w-]+" (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$ "//email address 

1. The dashed box when the Cancel button is pressed

Add attribute value in input hidefocus or hidefocus=true

2. Read-only text box contents

Add attribute value in input readonly

3. Prevent back-emptying of the text document (you can make the style content as a class reference)

<input Style=behavior:url (#default #savehistory); Type=text 

The 4.ENTER key allows the cursor to move to the next input box

 
 

5. Only for Chinese (with flashing)

<input onkeyup= "value="/value.replace (/["-~]/g,") " 

6. Only for numbers (with flashing)

<input onkeyup= "value="/value.replace (/["^\d]/g,") 

7. Only for numbers (no flashing)

<input ime-mode:disabled " 
onkeydown=" if (event.keycode==13) event.keycode=9 "onkeypress=" if 

8. Can only input English and digital (with flashing)

<input onkeyup= "value="/value.replace (/[\w]/g, "')" 

9. Shielding Input Method

<input type= "text" name= "url" ime-mode:disabled " 

10. Only digits, decimal points, minus (-) characters (no flashing) can be entered.

<input onkeypress= "If" (event.keycode!=46 && event.keycode!=45 && 

11. Can only enter two decimal places, three decimal places (with flashing)

<input maxlength=9 
onkeyup= "if (Value.match (/^\d{3}$/)) value="/value.replace (Value,parseint (VALUE/10)) " 
; value= "/value.replace (/\.\d*\./g, '.") "onkeypress=" if (event.keycode<48 
| | event.keycode>57) && event.keycode!=46 && event.keycode!=45 | | 
Value.match (/^\d{3}$/) | | /\.\d{3}$/.test (value)) 
{event.returnvalue=false} "Id=text_kfxe name=text_kfxe>

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.