input文本框輸入時正則判斷

1、只能輸入數字

文本框只能輸入數字代碼(小數點也不能輸入)

方式一:

<input type="text"name="number" id="number" value="1" maxlength="8" title="nb" onkeyup="value=value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g, ''))">

方式二:

<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"> 

2.只能輸入數字,能輸小數點.

方法一:

<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"> <input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能輸入數字');this.value='';}"> 

方法二 :

<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}"> 

3.只能輸入字母和漢字

<input onkeyup="value=value.replace(/[\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))" maxlength=10 name="Numbers"> 

4.只能輸入英文字母和數字,不能輸入中文

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

5.只能輸入數字和英文

方法一:

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

方法二:

<input onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">6.小數點後只能有最多兩位(數字,中文都可輸入),不能輸入字母和運算符號:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false"> 

7.小數點後只能有最多兩位(數字,字母,中文都可輸入),可以輸入運算符號:

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

8、禁止特殊字符:

onKeyPress="if(event.keyCode < 45 || event.keyCode > 57 ) event.returnValue = false;"

9、只能輸入漢字:

<input onkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^/u4E00-/u9FA5]/g,''))">

10、只禁止空格輸入

onkeyup="value=value.replace(/\s/g,'')"

11、只能輸入中文和英文:

onkeyup="value=value.replace(/[^\a-zA-Z\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\a-zA-Z\u4E00-\u9FA5]/g,''))"

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?"男":"女") }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章