1. 程式人生 > >input輸入框限制輸入英文,數字,漢字

input輸入框限制輸入英文,數字,漢字

get put 中文 repl turn value 下劃線 function fun

<h1>js驗證輸入框內容</h1>
<br />
<br />

只能輸入英文
<input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,‘‘)">
<br />
<br />
只能輸入英文
<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,‘‘)"
onkeydown="fncKeyStop(event)" onpaste="return false"
oncontextmenu="return false" />
無法粘貼,右鍵不會彈出粘貼菜單
<br />
<br />
只能輸入數字:
<input onkeyup="this.value=this.value.replace(/\D/g,‘‘)"
onafterpaste="this.value=this.value.replace(/\D/g,‘‘)">
<br />
<br />
只能輸入數字,小數點:
<input name="price" type="text"
onkeyup="value=value.replace(/[^\d\.]/g,‘‘)">
<br />
<br />
只能輸入數字,小數點,下劃線:
<input name="price" type="text"
onkeyup="value=value.replace(/[^\d\._]/g,‘‘)">
<br />
<br />
只能輸入英文和數字:
<input onkeyup="value=value.replace(/[\W]/g,‘‘) "
onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\d]/g,‘‘))">
<br />
<br />
只能輸入漢字:
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,‘‘)"
onbeforepaste="clipboardData.setData(‘text‘,clipboardData.getData(‘text‘).replace(/[^\u4E00-\u9FA5]/g,‘‘))">
<br />
<br />
禁止輸入法輸入:
<input type="text" style="ime-mode: disabled">
無法切換輸入法
<br />
<br />
[email protected]

/* */:
<input type="text"
onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,‘‘)">
<br />
<br />
不能為空:
<input onblur="if(this.value.replace(/^ +| +$/g,‘‘)==‘‘)alert(‘不能為空!‘)">
<br />
<br />

數字校驗有個bug,希望大家這邊註意一下,就是如果有input從後臺獲取的value值的話,上面的數字校驗失效,下面貼出此情況下的處理方式

<input id="lastyearwage" name="lastyearwage" type="text" class="input-text" value="${acheck.lastyearwage }" onkeyup="this.value=this.value.replace(/\D/g,‘‘)" onafterpaste="this.value=this.value.replace(/\D/g,‘‘)"/>

function keyPress() {
var keyCode = event.keyCode;
if ((keyCode >= 48 && keyCode <= 57))
{
event.returnValue = true;
} else {
event.returnValue = false;
}
}

input輸入框限制輸入英文,數字,漢字