首頁  >  文章  >  web前端  >  html中如何控制input輸入格式的範例

html中如何控制input輸入格式的範例

黄舟
黄舟原創
2017-10-27 10:15:025040瀏覽

只能輸入中文

<input type="text" onkeyup="this.value=this.value.replace(/[^\u4e00-\u9fa5]/g,&#39;&#39;)">

只能輸入英文

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

文字方塊只能輸入數字代碼(小數點也不能輸入)

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

只能輸入數字,能輸小數點

方法一:<input onkeyup="if(isNaN(value))execCommand(&#39;undo&#39;)" onafterpaste="if(isNaN(value))execCommand(&#39;undo&#39;)">  
方法二:<input name=txt1 onchange="if(/\D/.test(this.value)){alert(&#39;只能输入数字&#39;);this.value=&#39;&#39;;}">  
方法三:<input onkeyup="this.value=this.value.replace(/[^\d.]/g,&#39;&#39;)" onafterpaste="this.value=this.value.replace(/[^\d.]/g,&#39;&#39;)" >

只能輸入數字和英文

<input onKeyUp="value=value.replace(/[^\d|chun]/g,&#39;&#39;)">

只能輸入字母和中文

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

只能輸入字母和數字

<input onkeyup="value=value.replace(/[^\w\.\/]/ig,&#39;&#39;)">
<input type="text" size="20" onkeyup="this.value=this.value.replace(/[^\w_]/g,&#39;&#39;);">

這個可以輸入大小寫字母和數字,下畫線

<input type="text" size="20" onkeyup="this.value=this.value.replace(/[^\d-]/g,&#39;&#39;)" onafterpaste="this.value=this.value.replace(/[^\d-]/g,&#39;&#39;)">

這個可以輸入數字,中線(電話號碼)

<input type="text" size="20" onkeyup="this.value=this.value.replace(/[^a-z0-9_]/g,&#39;&#39;);">

這個可以輸入小寫字母和數字,下畫線

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

這個可以輸入數字和點

min、max 和step 屬性用於包含數字、日期的input 類型約束
min 屬性規定輸入域所允許的最小值。
max 屬性規定輸入域所允許的最大值。
step 屬性為輸入域規定合法的數字間隔

以上是html中如何控制input輸入格式的範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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