JS Limit text Box input number

Source: Internet
Author: User

1. Enter a positive integer greater than 0

<input onkeyup= "if (this.value.length==1) {this.value=this.value.replace (/[^1-9]/g, ')}else{this.value= This.value.replace (/\d/g, ')} "onafterpaste=" if (this.value.length==1) {this.value=this.value.replace (/[^1-9]/g, ' ')}else{this.value=this.value.replace (/\d/g, ')} ">

2, the text box can only enter a numeric code (the decimal point can not be entered)

<input onkeyup= "This.value=this.value.replace (/\d/g, ')" onafterpaste= "This.value=this.value.replace (/\D/g, ' ) ">

3, can only enter a number, to lose a decimal point.

<input onkeyup= "if (IsNaN (value)) ExecCommand (' Undo ')" Onafterpaste= "if (IsNaN (value)) ExecCommand (' Undo ')" >
<input name=txt1 onchange= "if (/\d/.test (this.value)) {alert (' Input number only '); this.value= ';}" >

4, digit and decimal method two

<input type=text t_value= "" o_value= "" onkeypress= "if (!this.value.match.? \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*?)? $/) 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} ">

Summary: Encapsulate as a separate function

function KeyPress (ob) {
if (!ob.value.match (/^[\+\-]?\d*?\.? \d*?$/)) Ob.value = Ob.t_value; else Ob.t_value = Ob.value; if (Ob.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)?)? $/)) Ob.o_value = Ob.value;
}
function KeyUp (ob) {
if (!ob.value.match (/^[\+\-]?\d*?\.? \d*?$/)) Ob.value = Ob.t_value; else Ob.t_value = Ob.value; if (Ob.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)?)? $/)) Ob.o_value = Ob.value;
}
function OnBlur (ob) {
if (!ob.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)? | \.\d*?)? $/) ob.value=ob.o_value;else{if (Ob.value.match (/^\.\d+$/)) ob.value=0+ob.value;if (Ob.value.match (/^\.$/)) Ob.value=0;ob.o_value=ob.value};
}

5. Only letters and kanji can be entered

<input onkeyup= "Value=value.replace (/[\d]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[\d]/g, ') "maxlength=10 name=" Numbers ">

6, can only enter English letters and numbers, can not input Chinese

<input onkeyup= "Value=value.replace (/[^\w\.\/]/ig, ')" >

7. Only enter numbers and English

<input onkeyup= "Value=value.replace (/[^\d|chun]/g, ')" >

8, the decimal point can only have a maximum of two digits (numbers, Chinese can be entered), can not enter letters and operation symbols:

<input onkeypress= "if ((event.keycode<48 | | event.keycode>57) && event.keycode!=46 | |/\.\d\d$/.test ( Value)) Event.returnvalue=false ">

9, after the decimal point can only have up to two digits (numbers, letters, Chinese can be entered), you can enter the operation symbol:

<input onkeyup= "This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ') ">

JS Limit text Box input 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.