只能输入数字和小数点的正则


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="" novalidate> 
<input type="text" autocomplete="off" id="price" autofocus="autofocus" onkeyup="value=value.replace(/[^\d^\.]+/g,'')">
</form>
</body>
</html>

以上代码其实不能保证只输入一个小数点,要想只输入一个小数点必须加上

replace('.','$#$').replace(/\./g,'').replace('$#$','.')  // 这里用到了普通字符串的替换

现在可以再测试下了! 

<input type="text" autocomplete="off" id="price" autofocus="autofocus" onkeyup="value=value.replace(/[^\d^\.]+/g,'').replace('.','$#$').replace(/\./g,'').replace('$#$','.')">

 

补充: 只能输入数字和一个小数点的正则表达式

var reg = /^\d+$|^\d*\.\d+$/g;
reg.test(val) 用于判断

 

 

 

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM