問題:
在做angular前端項目時要求時間輸入框只能輸入數字(小數點也不能輸入)。
實現:
在.html中用正則表達式來進行input的輸入限制。
代碼:
<input onkeyup="this.value=this.value.replace(/\D/g,'')"
onafterpaste="this.value=this.value.replace(/\D/g,'')">
this.value 表示此輸入框的值。/\D/g爲正則表達式,用來匹配所有非數字字符。
這句話的功能:將輸入爲非數字的字符替換爲空。
資源分享:
特殊字符大全:
http://xh.5156edu.com/page/18466.html
在線正則表達式測試:
http://tool.oschina.net/regex/#