当前位置:网站首页>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>边栏推荐
- ALC experiment
- Native JS takes you to understand the implementation and use of array methods
- 设置文本向两边居中展示
- D39_Eulerian Angles and Quaternions
- 云计算基础-学习笔记
- Browser Storage for H5
- Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station
- Detailed explanation of ten solutions across domains (summary)
- 超简单的白鹭egret项目添加图片详细教程
- Error correction notes for the book Image Processing, Analysis and Machine Vision
猜你喜欢
随机推荐
长度以及颜色单位基本概念
Detailed explanation of the construction process of Nacos cluster
Linux中安装Redis教程
The use of three parameters of ref, out, and Params in Unity3D
盒子模型小练习
LaTeX使用frame制作PPT图片没有标号
Quick Start to Drools Rule Engine (1)
Transformer详细解读与预测实例记录
el-progress implements different colors of the progress bar
Network Protocol Fundamentals - Study Notes
reduce()方法的学习和整理
指针常量与常量指针 巧记
DevOps - Understanding Learning
获取预训练模型的网络输入尺寸
numpy.random usage documentation
LaTeX image captioning text column automatic line wrapping
vs2017关于函数命名方面的注意事项
盒子模型大详解
One-arm routing experiment and three-layer switch experiment
System basics - study notes (some command records)









