Example of JS judging text box values
- 2020-03-30 02:21:02
- OfStack
JS judgment can only be Numbers and decimal points (excerpted from other sources and published here for personal reference only)
1. Text box can only enter Numbers (excluding decimal points)
< Input onkeyup = "this value = this. The value, the replace (\ D/g,") "onafterpaste =". This value = this. The value, the replace (\ D/g, ") ">
Only Numbers and decimal points can be entered.
< 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 (/ ^ [\ + \]? \ d *? \.? \ d *? $/)). This value = this. T_value; Value; else enclosing t_value = this. If (this. Value. The match (/ ^ (? : [\ + \]? \ d + (? : \ \ d +)? ? $/)) enclosing o_value = this. The value "onkeyup =" if (! Enclosing the value. The match (/ ^ [\ + \]? \ d *? \.? \ d *? $/)). This value = this. T_value; Value; else enclosing t_value = this. If (this. Value. The match (/ ^ (? : [\ + \]? \ d + (? : \ \ d +)? ? $/)) enclosing o_value = this. The value "onblur =" if (! This. The value. The match (/ ^ (? : [\ + \]? \ d + (? : \ \ d +)? . | \ \ d *? ? $/)). 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} ">
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 English letters and Numbers only, not Chinese
< Input onkeyup = "value = value. The replace (/ [^ \ w \ \] / ig,") ">
6. Enter only Numbers and English. Font color = "Red" > Chun< / font>
< Input onKeyUp = "value = value. The replace (/ [^ \ | d chun] / g,") ">
7. No more than two digits can be entered after the decimal point. No more letters or operational symbols can be entered:
< Input onKeyPress = "if ((event. KeyCode< 48 | | event. KeyCode> 57) && event keyCode! = 46 | | / \ \ d \ d $/ test (value)) event. ReturnValue = false ">
8. There can be no more than two digits after the decimal point (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)" >
It can only be Numbers and decimal points and add, subtract and multiply
Onkeypress = "return event. KeyCode> = 4 && event. KeyCode< = 57"
1. Text box can only enter Numbers (excluding decimal points)
< Input onkeyup = "this value = this. The value, the replace (\ D/g,") "onafterpaste =". This value = this. The value, the replace (\ D/g, ") ">
Only Numbers and decimal points can be entered.
< 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 (/ ^ [\ + \]? \ d *? \.? \ d *? $/)). This value = this. T_value; Value; else enclosing t_value = this. If (this. Value. The match (/ ^ (? : [\ + \]? \ d + (? : \ \ d +)? ? $/)) enclosing o_value = this. The value "onkeyup =" if (! Enclosing the value. The match (/ ^ [\ + \]? \ d *? \.? \ d *? $/)). This value = this. T_value; Value; else enclosing t_value = this. If (this. Value. The match (/ ^ (? : [\ + \]? \ d + (? : \ \ d +)? ? $/)) enclosing o_value = this. The value "onblur =" if (! This. The value. The match (/ ^ (? : [\ + \]? \ d + (? : \ \ d +)? . | \ \ d *? ? $/)). 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} ">
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 English letters and Numbers only, not Chinese
< Input onkeyup = "value = value. The replace (/ [^ \ w \ \] / ig,") ">
6. Enter only Numbers and English. Font color = "Red" > Chun< / font>
< Input onKeyUp = "value = value. The replace (/ [^ \ | d chun] / g,") ">
7. No more than two digits can be entered after the decimal point. No more letters or operational symbols can be entered:
< Input onKeyPress = "if ((event. KeyCode< 48 | | event. KeyCode> 57) && event keyCode! = 46 | | / \ \ d \ d $/ test (value)) event. ReturnValue = false ">
8. There can be no more than two digits after the decimal point (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)" >
It can only be Numbers and decimal points and add, subtract and multiply
Onkeypress = "return event. KeyCode> = 4 && event. KeyCode< = 57"