天天看點

控制input輸入格式

隻能輸入中文

<input type="text" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,'')">    

隻能輸入英文

<input type="text" onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')">    

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

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

隻能輸入數字,能輸小數點

方法一:<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 onkeyup="this.value=this.value.replace(/[^\d.]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d.]/g,'')" >  

隻能輸入數字和英文

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

隻能輸入字母和中文

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

 maxlength=10 name="Numbers">  

隻能輸入字母和數字

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

<input type="text" size="20" onkeyup="this.value=this.value.replace(/[^\w_]/g,'');"> 

這個可以輸入大小寫字母和數字,下畫線

<input type="text" size="20" onkeyup="this.value=this.value.replace(/[^\d-]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d-]/g,'')"> 

這個可以輸入數字,中線(電話号碼)

<input type="text" size="20" onkeyup="this.value=this.value.replace(/[^a-z0-9_]/g,'');"> 

這個可以輸入小寫字母和數字,下畫線

<input type="text" onkeyup="value=value.replace(/[^\d.]/g,'')">

這個可以輸入數字和點

min、max 和 step 屬性用于包含數字、日期的 input 類型限制。

min 屬性規定輸入域所允許的最小值。

max 屬性規定輸入域所允許的最大值。

step 屬性為輸入域規定合法的數字間隔

本文轉自 潛心笃志 51CTO部落格,原文連結:http://blog.51cto.com/82711020/1971803