ASK:寫(xiě)一個(gè)小程序,在input的type="text"里控制只能輸入數字,如果輸入其他字符則會(huì )出現提示框!請問(wèn)怎么做???
幾種方法:
| <script language=java script> <input onkeydown="onlyNum();"> |
| <script> function check(){ if (isNaN(tt.value)) {alert("非法字符!"); tt.value="";} } </script> <input type="text" name="tt" onkeyup="check();"> |
| <script language=jscript> function filterInput() function filterPaste() <input onkeypress="number()" onkeyup="filterInput()" onchange="filterInput()" onbeforepaste="filterPaste()" onpaste="return false" style="ime-mode: disabled"> |
4、允許小數點(diǎn)跟數字:
| <input onKeypress="return (/[\d.]/.test(String.fromCharCode(event.keyCode)))" style="ime-mode:Disabled"> |
| <input onkeypress="return(event.keyCode>=48&&event.keyCode<=57)" style="ime-mode:disabled" onbeforepaste="clipboardData.setData(’text’,clipboardData.getData(’text’).replace(/[^\d]/g,’’))"> |
6、只允許輸入漢字:
| <input onkeypress=return(false) onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,’’)"> |
7、推薦的:
| <input onkeypress="return event.keyCode>=48&&event.keyCode<=57" onpaste="return !clipboardData.getData(’text’).match(/\D/)" style="ime-mode:disabled" ondragenter="return false"> |
聯(lián)系客服