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


1.只能輸入英文

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

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

3.只能輸入數字:
  
<input onkeyup="this.value=this.value.replace(/\D/g,'')"
onafterpaste="this.value=this.value.replace(/\D/g,'')">

4.只能輸入數字,小數點:
  
<input name="price" type="text"
onkeyup="value=value.replace(/[^\d\.]/g,'')">

5.只能輸入數字,小數點,下划線:
<input name="price" type="text"
onkeyup="value=value.replace(/[^\d\._]/g,'')">

6.只能輸入英文和數字:
  
<input onkeyup="value=value.replace(/[\W]/g,'') "
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">

7.只能輸入漢字:
  
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')"
onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">

8.禁止輸入法輸入:
  
<input type="text" style="ime-mode: disabled">

9.只能輸入中文、英文、數字、@符號和.符號:
<input type="text"
onkeyup="value=value.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'')">

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
























免責聲明!

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



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