当前位置:网站首页>JS控制只能输入数字并且最多允许小数点两位
JS控制只能输入数字并且最多允许小数点两位
2022-08-05 05:29:00 【weixin_43923808】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<input type="text" name="je" onblur="clearNoNum(this)"/>元
<script type="text/javascript">
function clearNoNum(obj) {
obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
if (obj.value.indexOf(".") < 0 && obj.value != "") {//以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
obj.value = parseFloat(obj.value);
}
if (!obj.value || obj.value == '0' || obj.value == '0.0' || obj.value == '0.00') {
alert('退款金额不能为空');
return;
}
// 正常得话继续调后端接口
}
</script>
</body>
</html>
边栏推荐
猜你喜欢
LaTeX image captioning text column automatic line wrapping
格式化代码缩进的小技巧
Passing parameters in multiple threads
uniapp打包次数限制怎么办?只需两步就能解决
Writing OpenCV in VSCode
VRRP overview and experiment
Mina's long and short connections
Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
el-progress implements different colors of the progress bar
Transformer interprets and predicts instance records in detail
随机推荐
docker部署完mysql无法连接
MyCat配置文件
格式化代码缩进的小技巧
numpy.random usage documentation
Difference between link and @improt
Shadowless Cloud Desktop
Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"
reduce()方法的学习和整理
错误记录集锦(遇到则记下)
config.js related configuration summary
HelloWorld
Successful indie developers deal with failure & imposters
LaTeX 图片加标题 文本分栏自动换行
Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station
NACOS Configuration Center Settings Profile
前置++和后置++的区别
超简单的白鹭egret项目添加图片详细教程
获取预训练模型的网络输入尺寸
GetEnumerator method and MoveNext and Reset methods in Unity
多行文本省略