1. 程式人生 > >html input 只能輸入數字 只能輸入字母數字組合的正則表示式

html 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,'')"
>
只能輸入數字,能輸小數點
  1. 方法一:<input onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')">
  2. 方法二:<input name=txt1 onchange="if(/\D/.test(this.value)){alert('只能輸入數字');this.value='';}">
  3. 方法三:<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,'')">
只能輸入字母和中文
  1. <input onkeyup="value=value.replace(/[\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[\d]/g,''))"
  2. maxlength=10 name="Numbers">
只能輸入字母和數字
<input onkeyup="value=value.replace(/[^\w\.\/]/ig,'')"
>

min、max 和 step 屬性用於包含數字、日期的 input 型別約束。 min 屬性規定輸入域所允許的最小值。 max 屬性規定輸入域所允許的最大值。 step 屬性為輸入域規定合法的數字間隔