js验证input输入正整数 和 输入的金额小数点后保留两位(PC端键盘输入)
// 验证开头不为零的正整数
WST.zhengZhengShuIn = function (className){
var rex = /^[1-9]{1}[0-9]*$/;//正整数
$("."+className).keyup(function(event){
var event = event || window.event;
var value = $(this).val();
if(value.length>1){
var flagRex =/^0/;
if(flagRex.test(value)){
layer.msg('首个字符不能为0',{time:1500});
$(this).val('');
}
}
});
$("."+className).keypress(function(event){
var event = event || window.event;
if (event.charCode == 46 && event.charCode !== 0 && event.charCode < 48 || event.charCode > 57) {
return false;
}
if (/\./.test($(this).val()) && event.charCode === 46) {
return false;
}
if ($(this).val() === "" && event.charCode === 46) {
return false;
}
var tails = ($(this).val()).split(".")[1];
if (tails && tails.length === 2 && event.charCode !== 0) {
return false;
}
}); }
//验证 价格小数点后保留两位
WST.onlyPriceNum = function (obj) {
var priceRegex = /^\d+\.?\d{0,2}$/;
obj.blur(function(event) {
var event = event || window.event;
var val = $(this).val();
var newVal = val.replace(/[^\d.]/g, "");
newVal = newVal.replace(/^\./g, "");
newVal = newVal.replace(/\.{2,}/g, ".");
newVal = newVal.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
newVal = newVal.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
$(this).val(newVal);
});
obj.keyup(function(event) {
var event = event || window.event;
var val = $(this).val();
var newVal = val.replace(/[^\d.]/g, "");
newVal = newVal.replace(/^\./g, "");
newVal = newVal.replace(/\.{2,}/g, ".");
newVal = newVal.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
if (newVal.length > 0) {
var valArray = newVal.split(".");
var index = newVal.indexOf(".");
if (valArray.length === 1) {
if (index !== -1) {
newVal = parseInt(valArray[0]) + ".";
} else {
newVal = parseInt(valArray[0]) + "";
}
} else {
newVal = parseInt(valArray[0]) + "." + valArray[1];
}
}
newVal = newVal.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
$(this).val(newVal);
});
obj.keypress(function(event) {
if (event.charCode !== 46 && event.charCode !== 0 && event.charCode < 48 || event.charCode > 57) {
return false;
}
if (/\./.test($(this).val()) && event.charCode === 46) {
return false;
}
if ($(this).val() === "" && event.charCode === 46) {
return false;
}
var tails = ($(this).val()).split(".")[1];
if (tails && tails.length === 2 && event.charCode !== 0) {
return false;
}
});
}
js验证input输入正整数 和 输入的金额小数点后保留两位(PC端键盘输入)的更多相关文章
- 正则验证input输入,要求只能输入正数,小数点后保留两位。
<input type="number" step="1" min="0" onkeyup="this.value= thi ...
- 【js】小数点后保留两位小数
小数点后保留两位小数 dicountPrice.toFixed(2)
- js 让小数四舍五入保留两位小数的函数是?
js 让小数四舍五入保留两位小数的函数是? 例子:data.relations[i].data[j].toFixed(2) toFixed(2)这个函数就是保留两位小数的作用
- input只允许输入数字,并且小数点后保留4位
<input type="text" value="" name="should_send_num" id="should_ ...
- js实现小数点后保留N位并可以四舍五入——js对float数据的处理
曾经遇到的两次的问题,关于前台接受后台传过来的float数据,一显示就是老长的小数点后缀,很烦人,后来想着用js把其进行四舍五入处理下,经网上查找,一哥们的代码如下:(很好用,感谢下!) functi ...
- js小数点后保留几位方法:toFixed
(0.22223343534).toFixed(2) 结果:0.22
- JS限制input输入的为数字并且有小数的时候最多保留两位小数
JS限制input用户输入的为数字并且有小数的时候最多保留两位小数,代码如下: html部分: <input type="number" onkeypress="r ...
- 【前端开发】限制input输入保留两位小数
<input type="text" name='amount' id="cash_num" placeholder="请输入金额" ...
- js限制输入数字能输入小数点,js定义数组,js往数组中添加数据,js将字符型转为数字型,除法结果保留两位小数——js小测:计算比赛得分
一个朋友跟我说要去给某个比赛算分: 规则:去掉最低分最高分求平均分: 最近在学习大数据可视化——图谱,用到js一些东西,所以今天就用js练练 用到知识点: js限制输入数字能输入小数点,js定义数组, ...
随机推荐
- ubuntu18.04 安装hadoop 2.7.3+hive 2.3.4
1. 安装hadoop 详细请参见本人的另外一片博文<Hadoop 2.7.3 分布式集群安装> 2. 下载hive 2.3.4 解压文件到/opt/software -bin.tar.g ...
- How to Use vcpkg On Windows
Introduction If you do any sort of C++ development on Windows, then you know that library/package ma ...
- 简单工厂法( Factory Method)
工厂方法 (Factory Method) Define an interface for creating an object ,but let subclasses decide which cl ...
- python的高阶函数(map,reduce,filter)
Map函数 Map()函数接受两个参数,第一个参数是函数,第二个参数是序列(list,tuple),map将函数依次作用到序列上的每一个元素上,并发结果作为新的list返回 其中map的第一个参数的函 ...
- leetcode326
public class Solution { public bool IsPowerOfThree(int n) { && ( % n == ); } } https://leetc ...
- spring-boot + mybatis +pagehelper 使用分页
转自:https://segmentfault.com/a/1190000015668715?utm_medium=referral&utm_source=tuicool 最近自己搭建一个sp ...
- as2 针对加载进来的swf操作
如果加载的子swf,里面的东西会随时发生变化,那么as2获取的子swf宽高也会不停在变动. 解决方法,就是在子swf里面控制设置方法,当as2需要准确获取子swf宽高时,迫使子宽高恢复到原本样子,这样 ...
- C# 通用方法
一. /// <summary> /// 删除字符串中的中文 /// </summary> public static string Delete(string str) { ...
- vector实现(只能装入string)
#include<iostream> #include<string> #include<memory> #include<utility> using ...
- China cuts bank reserves by $100m to cushion US tariffs
China cuts bank reserves by $100m to cushion US tariffs中国央行定向降准释放千亿美元资金China is cutting the amount o ...