1、只能输入数字
文本框只能输入数字代码(小数点也不能输入)
方式一:
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text"</span><span class="atn">name</span><span class="pun">=</span><span class="atv">"number"</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"number"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"1"</span><span class="pln"> </span><span class="atn">maxlength</span><span class="pun">=</span><span class="atv">"8"</span><span class="pln"> </span><span class="atn">title</span><span class="pun">=</span><span class="atv">"nb"</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="pln">value</span><span class="pun">=</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[^d]/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">)</span><span class="atv">"</span><span class="pln"> </span><span class="atn">onbeforepaste</span><span class="pun">=</span><span class="atv">"</span><span class="pln">clipboardData</span><span class="pun">.</span><span class="pln">setData</span><span class="pun">(</span><span class="str">’text’</span><span class="pun">,</span><span class="pln">clipboardData</span><span class="pun">.</span><span class="pln">getData</span><span class="pun">(</span><span class="str">’text’</span><span class="pun">).</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[^d]/</span><span class="pln">g</span><span class="pun">,</span><span class="pln"> </span><span class="str">’’</span><span class="pun">))</span><span class="atv">"</span><span class="tag">></span></code>
方式二:
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/D/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">)</span><span class="atv">"</span><span class="pln"> </span><span class="atn">onafterpaste</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/D/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">)</span><span class="atv">"</span><span class="tag">></span><span class="pln"> </span></code>
2.只能输入数字,能输小数点.
方法一:
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">if</span><span class="pun">(</span><span class="pln">isNaN</span><span class="pun">(</span><span class="pln">value</span><span class="pun">))</span><span class="pln">execCommand</span><span class="pun">(</span><span class="str">’undo’</span><span class="pun">)</span><span class="atv">"</span><span class="pln"> </span><span class="atn">onafterpaste</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">if</span><span class="pun">(</span><span class="pln">isNaN</span><span class="pun">(</span><span class="pln">value</span><span class="pun">))</span><span class="pln">execCommand</span><span class="pun">(</span><span class="str">’undo’</span><span class="pun">)</span><span class="atv">"</span><span class="tag">></span><span class="pln"> </span><span class="tag"><input</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">txt1</span><span class="pln"> </span><span class="atn">onchange</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">if</span><span class="pun">(</span><span class="str">/D/</span><span class="pun">.</span><span class="pln">test</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">)){</span><span class="pln">alert</span><span class="pun">(</span><span class="str">’只能输入数字’</span><span class="pun">);</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="str">’’</span><span class="pun">;}</span><span class="atv">"</span><span class="tag">></span><span class="pln"> </span></code>
方法二 :
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">text</span><span class="pln"> </span><span class="atn">t_value</span><span class="pun">=</span><span class="atv">""</span><span class="pln"> </span><span class="atn">o_value</span><span class="pun">=</span><span class="atv">""</span><span class="pln"> </span><span class="atn">onkeypress</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">if</span><span class="pun">(!</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">/^[+-]?d*?.?d*?$/</span><span class="pun">))</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">t_value</span><span class="pun">;</span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">t_value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">;</span><span class="kwd">if</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">/^(?:[+-]?d+(?:.d+)?)?$/</span><span class="pun">))</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">o_value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="atv">"</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">if</span><span class="pun">(!</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">/^[+-]?d*?.?d*?$/</span><span class="pun">))</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">t_value</span><span class="pun">;</span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">t_value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">;</span><span class="kwd">if</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">/^(?:[+-]?d+(?:.d+)?)?$/</span><span class="pun">))</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">o_value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="atv">"</span><span class="pln"> </span><span class="atn">onblur</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">if</span><span class="pun">(!</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">/^(?:[+-]?d+(?:.d+)?|.d*?)?$/</span><span class="pun">))</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">o_value</span><span class="pun">;</span><span class="kwd">else</span><span class="pun">{</span><span class="kwd">if</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">/^.d+$/</span><span class="pun">))</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="lit">0</span><span class="pun">+</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">;</span><span class="kwd">if</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">match</span><span class="pun">(</span><span class="str">/^.$/</span><span class="pun">))</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">o_value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">}</span><span class="atv">"</span><span class="tag">></span><span class="pln"> </span></code>
3.只能输入字母和汉字
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="pln">value</span><span class="pun">=</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[d]/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">)</span><span class="pln"> </span><span class="atv">"</span><span class="atn">onbeforepaste</span><span class="pun">=</span><span class="atv">"</span><span class="pln">clipboardData</span><span class="pun">.</span><span class="pln">setData</span><span class="pun">(</span><span class="str">’text’</span><span class="pun">,</span><span class="pln">clipboardData</span><span class="pun">.</span><span class="pln">getData</span><span class="pun">(</span><span class="str">’text’</span><span class="pun">).</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[d]/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">))</span><span class="atv">"</span><span class="pln"> </span><span class="atn">maxlength</span><span class="pun">=</span><span class="atv">10</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"Numbers"</span><span class="tag">></span><span class="pln"> </span></code>
4.只能输入英文字母和数字,不能输入中文
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="pln">value</span><span class="pun">=</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[^w./]/</span><span class="pln">ig</span><span class="pun">,</span><span class="str">’’</span><span class="pun">)</span><span class="atv">"</span><span class="tag">></span><span class="pln"> </span></code>
5.只能输入数字和英文
方法一:
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">onKeyUp</span><span class="pun">=</span><span class="atv">"</span><span class="pln">value</span><span class="pun">=</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[^d|chun]/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">)</span><span class="atv">"</span><span class="tag">></span><span class="pln"> </span></code>
方法二:
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="pln">value</span><span class="pun">=</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[W]/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">)</span><span class="pln"> </span><span class="atv">"</span><span class="atn">onbeforepaste</span><span class="pun">=</span><span class="atv">"</span><span class="pln">clipboardData</span><span class="pun">.</span><span class="pln">setData</span><span class="pun">(</span><span class="str">’text’</span><span class="pun">,</span><span class="pln">clipboardData</span><span class="pun">.</span><span class="pln">getData</span><span class="pun">(</span><span class="str">’text’</span><span class="pun">).</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[^d]/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">))</span><span class="atv">"</span><span class="tag">></span></code>
6.小数点后只能有最多两位(数字,中文都可输入),不能输入字母和运算符号:
<code><span class="pln"><input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /.dd$/.test(value))event.returnValue=false"> </span></code>
7.小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号:
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">=</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/^(-)*(d+).(dd).*$/</span><span class="pun">,</span><span class="str">’$1$2.$3’</span><span class="pun">)</span><span class="atv">"</span><span class="tag">></span></code>
8、禁止特殊字符:
<code><span class="pln">onKeyPress</span><span class="pun">=</span><span class="str">"if(event.keyCode < 45 || event.keyCode > 57 ) event.returnValue = false;"</span></code>
9、只能输入汉字:
<code><span class="tag"><input</span><span class="pln"> </span><span class="atn">onkeyup</span><span class="pun">=</span><span class="atv">"</span><span class="pln">value</span><span class="pun">=</span><span class="pln">value</span><span class="pun">.</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[^/u4E00-/u9FA5]/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">)</span><span class="atv">"</span><span class="pln"> </span><span class="atn">onbeforepaste</span><span class="pun">=</span><span class="atv">"</span><span class="pln">clipboardData</span><span class="pun">.</span><span class="pln">setData</span><span class="pun">(</span><span class="str">’text’</span><span class="pun">,</span><span class="pln">clipboardData</span><span class="pun">.</span><span class="pln">getData</span><span class="pun">(</span><span class="str">’text’</span><span class="pun">).</span><span class="pln">replace</span><span class="pun">(</span><span class="str">/[^/u4E00-/u9FA5]/</span><span class="pln">g</span><span class="pun">,</span><span class="str">’’</span><span class="pun">))</span><span class="atv">"</span><span class="tag">></span></code>
10、只禁止空格输入
<code><span class="pln">onkeyup</span><span class="pun">=</span><span class="str">"value=value.replace(/s/g,’’)"</span></code>
11、只能输入中文和英文:
<code><span class="pln">onkeyup</span><span class="pun">=</span><span class="str">"value=value.replace(/[^a-zA-Zu4E00-u9FA5]/g,’’)"</span><span class="pln"> onbeforepaste</span><span class="pun">=</span><span class="str">"clipboardData.setData(’text’,clipboardData.getData(’text’).replace(/[^a-zA-Zu4E00-u9FA5]/g,’’))"</span></code>
12、身份证的正则判断:
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"} function isCardID(sId){ var iSum=0 ; var info="" ; if(!/^d{17}(d|x)$/i.test(sId)){ alert("你输入的身份证长度或格式错误"); flagsid=true;return;} sId=sId.replace(/x$/i,"a"); if(aCity[parseInt(sId.substr(0,2))]==null){ alert("你的身份证地区非法"); flagsid=true;return;} sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2)); var d=new Date(sBirthday.replace(/-/g,"/")) ; if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){alert("身份证上的出生日期非法") ; flagsid=true;return;} for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11) ; if(iSum%11!=1) {alert("你输入的身份证号非法"); flagsid=true;return;} flagsid=false; return true;//aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女") }
http://www.haorooms.com/post/js_regexp