"Turn" JS and regular expression control input only enter numeric or decimal problem Resolution

Source: Internet
Author: User

First: limits can only be integers

?
1 <input type = "text"name= "number" id = ‘number‘ onkeyup= "if(! /^d+$/.test(this.value)){alert(‘只能整数‘);this.value=‘‘;}"/>

If it's not an integer, alert directly.

Second: The limit is two decimal places

?
1 <input type = "text"name= "price" id = ‘price‘ onkeyup= "if( ! /^d*(?:.d{0,2})?$/.test(this.value)){alert(‘只能输入数字,小数点后只能保留两位‘);this.value=‘‘;}"/>

Principle:

Judging by regular expressions, the execution alert is not satisfied.

The first regular expression is/^d+$/that can be one or more numbers

The second regular expression is

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

Indicates that the number must begin with the number ending.

The emphasis here is to end the number, which is usually 1 decimal in the computer. , 2. This is the correct way to do this, but at the end of the decimal point. Here forces the number to end.

Test ()

Just find the part that is satisfied and return to true.

It means yes.

?
123 /d/. test ( ‘a‘) // false/d/. test ( ‘a‘ ) // true/d/. test ( ‘a‘) // true

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

The following is to introduce JS regular limit input box to enter the common code

1. Enter only numbers and English:

?
123 <input onkeyup="value=value.replace(/[\W]/g,‘‘) "onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))"ID="Text1"NAME="Text1">

2. Only numbers can be entered:

?
123 <input onkeyup="value=value.replace(/[^\d]/g,‘‘) "onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))"ID="Text2"NAME="Text2">

3. Only full-width input:

?
123 <input onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,‘‘)"onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\uFF00-\uFFFF]/g,‘‘))"ID="Text3"NAME="Text3">

4. Only the Chinese characters can be entered:

?
123 <input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,‘‘)"onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\u4E00-\u9FA5]/g,‘‘))"ID="Text4"NAME="Text4">

5. Email Address verification:

?
123456789 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 and legitimate e-mail address!) " ) return false

6. Identity card:

?
12345678910111213141516171819 "^\\d{17}(\\d|x)$"7.17种正则表达式 "^\\d+$"  //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$"  //正整数 "^((-\\d+)|(0+))$"  //非正整数(负整数 + 0) "^-[0-9]*[1-9][0-9]*$"  //负整数 "^-?\\d+$"    //整数 "^\\d+(\\.\\d+)?$"  //非负浮点数(正浮点数 + 0) "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"  //正浮点数 "^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$"  //非正浮点数(负浮点数 + 0) "^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"  //负浮点数 "^(-?\\d+)(\\.\\d+)?$"  //浮点数 "^[A-Za-z]+$"  //由26个英文字母组成的字符串 "^[A-Z]+$"  //由26个英文字母的大写组成的字符串 "^[a-z]+$"  //由26个英文字母的小写组成的字符串 "^[A-Za-z0-9]+$"  //由数字和26个英文字母组成的字符串 "^\\w+$"  //由数字、26个英文字母或者下划线组成的字符串 "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"    //email地址 "^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$"  //url

1. The dashed box when the button is pressed

Add attribute value Hidefocus or hidefocus=true in input

2. Read-only text box contents

Add the attribute value in input readonly

3. Prevent the text document from being emptied (you can use the style content as a class reference)

?
12  <INPUT style=behavior:url(#default#savehistory); type=text id=oPersistInput>

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

?
1 <input onkeydown="if(event.keyCode==13)event.keyCode=9">

5. Only Chinese (with flashing)

?
12  <input onkeyup="value="/value.replace(/[" -~]/g,‘‘)"onkeydown="if(event.keyCode==13)event.keyCode=9">

6. Only digital (with flashing)

?
12   <input onkeyup="value="/value.replace(/["^\d]/g,‘‘) "onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))">

7. Only digital (no flashing)

?
123 <input ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeypress="if((event.keyCode<48 || event.keyCode>57)) event.returnValue=false">

8. Only input English and numerals (with flashing)

?
12 <input onkeyup="value="/value.replace(/[\W]/g,"‘‘)"onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))">

9. Shielding Input Method

?
12   <input type="text"name="url"ime-mode:disabled" onkeydown="if(event.keyCode==13)event.keyCode=9">

10. Enter only digits, decimal point, minus (-) character (no flashing)

?
12  <input onKeyPress="if (event.keyCode!=46 && event.keyCode!=45 && (event.keyCode<48 || event.keyCode>57)) event.returnValue=false">

11. Only two decimal places can be entered, three decimal places (with flashing)

?
123456 < Code class= "JS plain" ><input maxlength=9 onkeyup= " if (Value.match (/^\d{3}$/)) value= " /value.replace (Value,parseint (VALUE/10)) " ;value= " /value.replace (/\.\d*\./g, " 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>

"Go" JS and regular expression control input only enter numeric or decimal problem resolution

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.