The input box in HTML or PHP can only enter a positive integer, and the logical and/or operation

Source: Internet
Author: User

Sometimes you need to limit the type of text box input content, this section share the regular expression limit text box can only enter numbers, decimal points, English letters, Chinese characters and other code.

For example, enter a positive integer greater than 0

The code is as follows: <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, ')} ">


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

The code is as follows:
<input onkeyup= "This.value=this.value.replace (/\d/g, ')" onafterpaste= "This.value=this.value.replace (/\D/g, ' ) ">

2, only the number can be entered, the decimal point will be lost.

The code is as follows:
<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= ';}" >

3, digit and decimal method two

The code is as follows:
<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} ">

Encapsulated as a separate function:

The code is as follows: 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};
}

Just call, pass in the This object!

4, only letters and kanji can be entered

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

5, can only input English letters and numbers, cannot input Chinese

The code is as follows:

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

6, can only enter numbers and English

The code is as follows:
<input onkeyup= "Value=value.replace (/[^\d|chun]/g, ')" >

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

The code is as follows:
<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:

The code is as follows:
<input onkeyup= "This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ') "> 9, enter key to move the cursor to the next input box
<input type= "text" onkeydown= "if (event.keycode==13) event.keycode=9"/> 10, regular match ^[1-9]\d*$//matched positive integer
^-[1-9]\d*$//Match negative integer
^-? [1-9]\d*$//Match integer
^[1-9]\d*|0$//matches non-negative integers (positive integers + 0)
^-[1-9]\d*|0$//matches a non-positive integer (negative integer + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$//Match positive floating point number
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*) $//Match negative floating point number
^-? ([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0) $//Match floating point number
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$//Match non-negative floating-point number (positive floating point + 0)
^ (-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)) |0?\.0+|0$//match non-positive floating-point number (negative floating-point number + 0)

The input box in HTML or PHP can only enter a positive integer, and the logical and/or operation

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.