当前位置:网站首页>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>边栏推荐
猜你喜欢

BIO, NIO, AIO practical study notes (easy to understand theory)

白鹭egret添加新页面教程,如何添加新页面

LaTeX 图片加标题 文本分栏自动换行
盒子模型小练习

Cloud Computing Basics - Study Notes

el-progress implements different colors of the progress bar

BIO,NIO,AIO实践学习笔记(便于理解理论)

滚动条问题,未解决

NACOS Configuration Center Settings Profile

Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive
随机推荐
前置++和后置++的区别
【FAQ】什么是 Canon CCAPI
What is Alibaba Cloud Express Beauty Station?
LeetCode中常用语言的一些基本方法记录
单片机期末复习大题
【FAQ】CCAPI Compatible EOS Camera List (Updated in August 2022)
记录vue-页面缓存问题
多用户商城多商户B2B2C拼团砍价秒杀支持小程序H5+APP全开源
docker部署完mysql无法连接
vscode笔记
Transformer interprets and predicts instance records in detail
cs231n学习记录
获取预训练模型的网络输入尺寸
Mina disconnects and reconnects
ALC experiment
Pytorch分布式并行处理
js判断文字是否超过区域
System basics - study notes (some command records)
【考研结束第一天,过于空虚,想对自己进行总结一下】
wc, grep, tar, vi/vim