Regular expression restricts the text box to only input numbers
Author:Eve Cole
Update Time:2009-06-20 16:52:58
Many times we need to limit the type of input content in the text box when making forms. Below we use regular expressions to limit the text box to only input numbers, decimal points, English letters, Chinese characters and other codes.
1. Only numeric codes can be entered in the text box (decimal points cannot be entered either)
<input onkeyup="this.value=this.value.replace(/D/g,'')" onafterpaste="this.value=this.value.replace(/D/g,'')">
2. Only numbers can be entered, 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('Only numbers can be entered');this.value='';}">
3. Numbers and decimal point method 2
<input type=text t_value="" o_value="" onkeypress="if(!this.value.match(/^[+-]?d*?.?d*?$/))this .value=this.t_value;else this.t_value=this.value;if(this.value.match(/^(?:[+-]?d+(?:.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+(?:.d+)?)?$/) )this.o_value=this.value" onblur="if(!this.value.match(/^(?:[+-]?d+(?:.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}">
4. Only letters and Chinese characters can be entered
<input onkeyup="value=value.replace(/[d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[d] /g,''))" maxlength=10 name="Numbers">
5. Only English letters and numbers can be entered, but Chinese characters cannot be entered.
<input onkeyup="value=value.replace(/[^w./]/ig,'')">
6. Only numbers and English can be entered
<input onKeyUp="value=value.replace(/[^d|chun]/g,'')">
7. There can only be a maximum of two digits after the decimal point (both numbers and Chinese characters can be entered). Letters and arithmetic symbols cannot be entered:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /.dd$/.test(value))event.returnValue=false ">
8. There can only be up to two digits after the decimal point (numbers, letters, and Chinese characters can be entered), and arithmetic symbols can be entered:
<input onkeyup="this.value=this.value.replace(/^(-)*(d+).(dd).*$/,'$1$2.$3')">