Sometimes you need to restrict the type of content input in the text box. In this section, the regular expression restricts the text box to only enter numbers, decimal points, English letters, Chinese characters and other codes.
For example, enter a positive integer greater than 0
<input onkeyup="if(==1){=(/[^1-9]/g,'')}else{=(/\D/g,'')}" onafterpaste="if(==1){=(/[^1-9]/g,'')}else{=(/\D/g,'')}">
1. Only numeric codes can be entered in the text box (the decimal point cannot be entered either)
<input onkeyup="=(/\D/g,'')" onafterpaste="=(/\D/g,'')">
2. Only enter numbers and can enter decimal points. IE only
<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
<input name=txt1 onchange="if(/\D/.test()){alert('Only enter numbers');='';}">
3. Number and decimal point method two
<input type=text t_value="" o_value="" onkeypress="if(!(/^[\+\-]?\d*?\.?\d*?$/))=this.t_value;else this.t_value=;if((/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=" onkeyup="if(!(/^[\+\-]?\d*?\.?\d*?$/))=this.t_value;else this.t_value=;if((/^(?:[\+\-]?\d+(?:\.\d+)?)?$/))this.o_value=" onblur="if(!(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))=this.o_value;else{if((/^\.\d+$/))=0+;if((/^\.$/))=0;this.o_value=}">
Encapsulated into separate functions:
function keyPress(ob) { if (!(/^[\+\-]?\d*?\.?\d*?$/)) = ob.t_value; else ob.t_value = ; if ((/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)) ob.o_value = ; } function keyUp(ob) { if (!(/^[\+\-]?\d*?\.?\d*?$/)) = ob.t_value; else ob.t_value = ; if ((/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)) ob.o_value = ; } function onBlur(ob) { if(!(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/))=ob.o_value;else{if((/^\.\d+$/))=0+;if((/^\.$/))=0;ob.o_value=}; }
Just pass this object in the call!
4. Only enter letters and Chinese characters
<input onkeyup="value=(/[\d]/g,'') "onbeforepaste="('text',('text').replace(/[\d]/g,''))" maxlength=10 name="Numbers">
5. Only enter English letters and numbers, not Chinese
<input onkeyup="value=(/[^\w\.\/]/ig,'')">
6. Only enter numbers and English
<input onKeyUp="value=(/[^\d|chun]/g,'')">
7. There can only be up to two digits after the decimal point (numbers and Chinese can be entered), and letters and operator symbols cannot be entered:
<input onKeyPress="if((<48 || >57) && !=46 || /\.\d\d$/.test(value))=false">
8. There can only be up to two digits after the decimal point (numbers, letters, and Chinese), and operator symbols can be entered:
<input onkeyup="=(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
This is all about this article. Friends who need it can refer to it.