基础函数
// 正小数,小数点后两位,数值最前不能有0。
//'(\.[0-9]{1,2})?$)'修改其中的'2',可以改变小数点后位数的验证
var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
var money = "520.100";
console.log(reg.test(money));//true
//000 错
//0 对
//0. 错
//0.0 对
//050 错
//00050.12错
//70.1 对
//70.11 对
//70.111错
//500 正确
function returnFloat(value) {
var value = Math.round(parseFloat(value) * 100) / 100;
var xsd = value.toString().split(".");
if (xsd.length == 1) {
value = value.toString() + ".00";
return value;
}
if (xsd.length > 1) {
if (xsd[1].length < 2) {
value = value.toString() + "0";
}
return value;
}
}
function checkMobile(phoneNum) {
var reg_phone = /^[1][0-9][0-9]{9}$/;
var regPhone = new RegExp(reg_phone);
if (regPhone.test(phoneNum)) {
return true;
} else {
return false;
}
}
var countdown = 60;
function settime(obj) {
if (countdown == 0) {
obj.removeAttr("disabled").removeClass('weui-btn_disabled').text("获取验证码");
countdown = 60;
return;
} else {
obj.attr('disabled', true).addClass('weui-btn_disabled').text("重新发送(" + countdown + ")");
countdown--;
}
setTimeout(function () {
settime(obj);
}, 1000);
}
$(".js-btn-get-code").on('click', function () {
var $btn = $(this);
var tel_val=$("#mobile").val();
if(checkMobile(tel_val)===true){
settime($btn);
}
});
判断输入完成
- 每次输入都进行判断,(可用于验证成功时,执行其他函数,比如给成功提示,或者改变状态)
因为正则的关系且是每次输入完成就验证,导致输入到小数点’.’的时候就会报错。(解决办法是修改正则,可惜本人正则方面的知识很捉急)
$("#moneyCustom").bind('input propertychange', function () {
var moneyCustom = $(this).val();
var moneyCustom_end = returnFloat(moneyCustom);
console.log(moneyCustom);
if (moneyCustom) {
var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
if (!reg.test(moneyCustom)) {
$.alert({
title: '提示信息',
text: '赞赏金额的输入格式不正确,请确认!'
});
$(this).val("");
$("#moneyTotal").val("0.00");
changeNum();
return;
}
$("#moneyTotal").val(moneyCustom_end);
} else {
$("#moneyTotal").val("0.00");
}
$(".js-reward-list").find("li.active").removeClass("active").end().find(".iconfont").remove();
changeNum();
});
假设用户输入完成后,一段时间没操作,就判断为输入完成
var queue;
$("#moneyCustom").on('keyup', function (e) {
var $this=$(this);
clearTimeout(queue);
queue = setTimeout(function () {
var moneyCustom = $this.val();
var moneyCustom_end = returnFloat(moneyCustom);
console.log(moneyCustom);
if (moneyCustom) {
var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
if (!reg.test(moneyCustom)) {
$.alert({
title: '提示信息',
text: '打赏金额只能保留小数点后两位哦~'
});
$this.val("");
$("#moneyTotal").val("0.00");
changeNum();
return;
}
$("#moneyTotal").val(moneyCustom_end);
} else {
$("#moneyTotal").val("0.00");
}
$(".js-reward-list").find("li.active").removeClass("active").end().find(".iconfont").remove();
changeNum();
}, 1000);
});