首頁  >  文章  >  web前端  >  JS驗證input輸入框(字母,數字,符號,中文)的代碼實例分享

JS驗證input輸入框(字母,數字,符號,中文)的代碼實例分享

黄舟
黄舟原創
2017-03-24 14:28:401858瀏覽

本文主要介紹了JS驗證input輸入框(字母,數字,符號,中文)的方法。具有很好的參考價值。下面跟著小編一起來看下吧

只能輸入英文

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

只能輸入英文
##

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

無法貼上,右鍵不會彈出貼上選單

只能輸入數字:

<input onkeyup="this.value=this.value.replace(/\D/g,&#39;&#39;)"
onafterpaste="this.value=this.value.replace(/\D/g,&#39;&#39;)">

只能輸入數字,小數點:

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

只能輸入數字,小數點,底線:

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

只能輸入英文和數字:

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

只能輸入漢字:

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

禁止輸入法輸入:

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

無法切換輸入法

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

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

不能為空:

<input 
onblur
="
if
(this.value.replace(/^ +| +$/g,&#39;&#39;)==&#39;&#39;)alert(&#39;不能为空!&#39;)">

以上是JS驗證input輸入框(字母,數字,符號,中文)的代碼實例分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn