JS驗證input輸入框(字母,數字,符號,中文)正則實現


JS驗證input輸入框(字母,數字,符號,中文)

轉自http://www.cnblogs.com/huzhangjun/p/6605933.html

js驗證輸入框內容

只能輸入英文

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

只能輸入英文

<input type="text" onkeyup="value=value.replace(/[^\a-\z\A-\Z]/g,'')"
onkeydown="fncKeyStop(event)" onpaste="return false"
oncontextmenu="return false" />

無法粘貼,右鍵不會彈出粘貼菜單

只能輸入數字:

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

只能輸入數字,小數點:

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


只能輸入數字,小數點,下划線:

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

 

只能輸入英文和數字:

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


只能輸入漢字:

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


禁止輸入法輸入:

<input type="text" style="ime-mode: disabled">

無法切換輸入法

只能輸入中文、英文、數字、@符號和.符號:

<input type="text"
onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'')">


不能為空:

<input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能為空!')">

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM