请问如何不让type=number的Input,不可输入加号减号和多个小数点?

先谢谢各位了,我是写了一个type = number的Input,鉴于业务需求,这个Input中只能输入数字,加减号不可输入。
本来想判断input中的value值来手动删掉里面的加号减号及多余的点,可是又发现一旦输入的值不合法,Input的value已经被转化为空字符串 ‘’。这样我就无法取到input中的值了。
想了许久不知道解决方法,求各位大神帮忙了,谢谢~
对了,我是用vue写的页面~~~

慕容3067478
浏览 2161回答 1
1回答

德玛西亚99

用正则匹配 '^[-]?[0-9]*\.?[0-9]+(eE?[0-9]+)?$'<input&nbsp;type="text"&nbsp;/>可以用这种方式,监听input值的变化,发现验证不过就提示。<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Welcome</title></head><body><form>&nbsp; &nbsp; <input id="inputNumber" type="text" name="test" onblur="checkNumber()"></form><script type="text/javascript">&nbsp;function checkNumber(){&nbsp; &nbsp; var inputNumber = document.getElementById('inputNumber').value;&nbsp; &nbsp; if(!/^[-]?[0-9]*\.?[0-9]+(eE?[0-9]+)?$/.test(inputNumber)){&nbsp; &nbsp; &nbsp; &nbsp; alert('Please input a valid number!');&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}</script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript