Use js to make the input box input only contain numbers

Source: Internet
Author: User

JS judges that only numbers and decimal points are allowed. 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 are allowed, and decimal points are allowed. <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. method 2: Number and decimal point <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. you can only enter English letters and numbers, but cannot enter Chinese <input onkeyup = "value = value. replace (/[^ \ w \. \/]/Ig, '')"> 6. only numbers and English <font color = "Red"> chun </font> <input onKeyUp = "value = value. replace (/[^ \ d | chun]/g, '')"> 7. A maximum of two digits (numbers and Chinese characters) can be entered after the decimal point. 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. You can enter the operator number: <input onkeyup = "this. value = this. value. replace (/^ (\-) * (\ d + )\. (\ d ). * $/, '$1 $2. $3 ') "> it can only be a number or a decimal point, plus or minus onkeypress =" return event. keyCode> = 4 & event. keyCode <= 57"

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.