问题:
在做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/#