Js limits text boxes to numeric method summaries

  • 2020-03-30 03:20:32
  • OfStack

Sometimes you need to restrict the type of text field input. In this section, you can only enter Numbers, decimal points, English letters, Chinese characters, etc.

For example, enter a positive integer greater than 0

< Input onkeyup = "if (this. Value. Length = = 1) {this. Value = this. The value, the replace (/ / g ^ 1-9, ' ')} else {this. Value = this. The value, the replace (\ D/g, ' ')}" Onafterpaste = "if (this. Value. Length = = 1) {this. Value = this. The value, the replace (/ / g ^ 1-9, ' ')} else {this. Value = this. The value, the replace (\ D/g, ' ')}" >

1, text box can only enter numeric code (decimal point can not enter)

< Input onkeyup = "this value = this. The value, the replace (\ D/g,") "onafterpaste =". This value = this. The value, the replace (\ D/g, ") ">

2, can only enter a number, can enter a decimal point. IE only

< Input onkeyup = "if (isNaN (value)) execCommand (' undo ')" onafterpaste = "if (isNaN (value)) execCommand (' undo ')" >
< Input name=txt1 onchange="if(/\D/.test(this.value)){alert(' number only '); This. Value = ' '; } ">

3. Number and decimal point method two

< Input type=text t_value="" o_value="" onkeypress="if(! Enclosing the value. The match (/ ^ [\ + \] & # 63; \ d * & # 63; \. & # 63; \ d * & # 63; $/)). This value = this. T_value; Value; else enclosing t_value = this. If (this. Value. The match (/ ^ (& # 63; : [\ + \] & # 63; \ d + (& # 63; : \ \ d +) & # 63;) The & # 63; $/)) enclosing o_value = this. The value "onkeyup =" if (! Enclosing the value. The match (/ ^ [\ + \] & # 63; \ d * & # 63; \. & # 63; \ d * & # 63; $/)). This value = this. T_value; Value; else enclosing t_value = this. If (this. Value. The match (/ ^ (& # 63; : [\ + \] & # 63; \ d + (& # 63; : \ \ d +) & # 63;) The & # 63; $/)) enclosing o_value = this. The value "onblur =" if (! This. The value. The match (/ ^ (& # 63; : [\ + \] & # 63; \ d + (& # 63; : \ \ d +) & # 63; . | \ \ d * & # 63;) The & # 63; $/)). This value = this. O_value; Else {the if (this. Value. The match (/ ^ \ \ d + $/)). This value = 0 + enclosing the value; If (this. Value. The match (/ ^ \. $/)). This value = 0; Enclosing o_value = this. Value} ">

Encapsulated 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};
}

Just pass in this object in the call!

4. Only input letters and Chinese characters

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

5. Input only English letters and Numbers, not Chinese

< Input onkeyup = "value = value. The replace (/ [^ \ w \ \] / ig,") ">

6. Enter only Numbers and English

< Input onKeyUp = "value = value. The replace (/ [^ \ | d chun] / g,") ">

7. After the decimal point, there can be no more than two digits (digits can be input in Chinese), and no letters or operational symbols can be input:

< Input onKeyPress = "if ((event. KeyCode< 48 | | event. KeyCode> 57) && event keyCode! = 46 | | / \ \ d \ d $/ test (value)) event. ReturnValue = false ">

8. After the decimal point, there can be no more than two digits (digits, letters and Chinese characters can be input). Operation symbols can be input:

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

This article is introduced here, the need for a friend can refer to


Related articles: