input 各種限制
阿新 • • 發佈:2018-11-28
test
1.限制只能輸入或黏貼11位長度的數字
<input onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')" maxlength="11"/>
2.限制只能輸入或黏貼10位長度的漢字
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"maxlength="10">
3.限制只能輸入或黏貼10位長度的數字或英文 (數字和英文個數合計10個)
<input onkeyup="value=value.replace(/[\W]/g,'') " onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))" maxlength="10">
4.限制只能輸入或黏貼英文字母(10個)
<input type="text" onkeyup="this.value=this.value.replace(/[^a-zA-Z]/g,'')" maxlength="10">
5.限制只能輸入或黏貼數字或帶小數點數字
<input onkeyup="value=value.replace(/[^\d{1,}\.\d{1,}|\d{1,}]/g,'')" />
6.只能輸入數字和字母和’ - ‘號 用於輸入時間
<input onkeyup="value=value.replace(/[^\w&=]|_/ig,'')" onblur="value=value.replace(/[^\w&-]|_/ig,'')"/>