使用js正則控制input標籤只允許輸入的值

來源:互聯網
上載者:User

style="ime-mode:Disabled“
這句是比較實用的。意為關閉IME。省得有些人開著全形輸入數字,結果輸入不進去來找你哭天抹淚的,還怪你設計的不好。

只允許輸入數字
複製代碼 代碼如下:
<input name="username" type="text" onkeyup="value=this.value.replace(//D+/g,'')">

只允許輸入英文字母、數字和底線(以下二種方法實現)
複製代碼 代碼如下:
<input name="username" type="text" style="ime-mode:disabled">
<input name="username" type="text" onkeyup="value=value.replace(/[^/w/.//]/ig,'')">

只允許輸入英文字母、數字和&=@
複製代碼 代碼如下:
<input name="username" type="text" onkeyup="value=value.replace(/[^/w=@&]|_/ig,'')">

只允許輸入漢字
複製代碼 代碼如下:
<input name="username" type="text" onkeyup="value=value.replace(/[^/u4E00-/u9FA5]/g,'')">

執行個體:限制輸入三位 只能是數字或者'.' 並在input內關閉IME
複製代碼 代碼如下:
<th style="width: 60px;">折  扣</th>
<SPAN style="WHITE-SPACE: pre">
</SPAN><td><input name="discountNum" class="easyui-validatebox" maxlength="3" style="ime-mode:Disabled" onkeyup="value=this.value.replace(/[^\d.]/g,'')" data-options="required:'true',missingMessage:'請填寫折扣數'" />
<SPAN style="WHITE-SPACE: pre">
</SPAN></td>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.