JS Limit text box input can only enter a number

Source: Internet
Author: User

JS can only be judged by numbers and decimal points. 1, the text box can only enter a numeric code (the decimal point cannot be entered) to copy code examples:<input onkeyup="This.value=this.value.replace (/\d/g, ")"Onafterpaste="This.value=this.value.replace (/\d/g, ")">2, only numbers can be entered, and the decimal point will be lost. Copy Code code example:<input onkeyup="if (IsNaN (value)) ExecCommand (' Undo ')"Onafterpaste="if (IsNaN (value)) ExecCommand (' Undo ')"><input Name=txt1 onchange="if (/\d/.test (this.value)) {alert (' can only enter a number '); this.value= ";}">3, number and decimal method two copy Code code example:<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+)?)? $/)) 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+)?)? $/)) 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, you can only enter alphabetic and Kanji copy Code code examples:<input onkeyup="Value=value.replace (/[\d]/g, ")"Onbeforepaste="clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[\d]/g, ")"Maxlength=TenName="Numbers">5, you can only enter English letters and numbers, you cannot enter the Chinese copy Code code example:<input onkeyup="Value=value.replace (/[^\w\.\/]/ig, ")">6, only numbers and English <font can be entered color="Red">chun</font>Copy Code code example:<input onkeyup="Value=value.replace (/[^\d|chun]/g, ")">7, you can only have up to two digits after the decimal point (numbers, Chinese can be entered), you cannot enter letters and arithmetic symbols: Copy code example:<input onkeypress="if ((event.keycode<48 | | event.keycode>57) && event.keycode!=46 | |/\.\d\d$/.test (value)) Event.returnvalue=false">8, only a maximum of two digits after the decimal point (numbers, letters, Chinese can be entered), you can enter the operation symbol: Copy code code example:<input onkeyup="This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ')">can only be numeric and decimal and plus minus multiply the code code example: onkeypress="return event.keycode>=4&&event.keycode<=57"This article original link: http://www.jbxue.com/article/15871.html

JS Limit text box input can only enter a number

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.