一 .字符串替换
/**
* 字符串替换
* @param {string} str 要被替换的字符串
* @param {string} substr 要替换的字符串
* @param {string} newstr 用于替换的字符串
* @return {string} 替换后的新字符串
*/
function replace(str, substr, newstr) {
substr = substr.replace(/[.\\[\]{}()|^$?*+]/g, "\\$&"); // 转义字符串中的元字符
var re = new RegExp(substr, "g"); // 生成正则
return str.replace(re, newstr);
}
二.验证手机号码
function checkPhone(){
if (!(/^1[3456789]\d{9}$/.test(phone))
{
return false
}
else{
return true
}
}
三.验证输入金额
金额个数如:1、只保留两位小数 2、输入的数字中只能有一个小数点 3、首位不能为0
const exp = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
if (!exp.test(that.data.moneyCZ)) {
wx.showModal({
title: '提示',
content: '请输入正确的金额(最多保留2位小数)',
showCancel: false,//是否显示取消按钮
confirmText:"确定",//默认是“确定”
confirmColor: '#FF5555',//确定文字的颜色
success: function (res) {
console.log(res)
if(res.confirm){
that.setData({
moneyCZ:''
})
}
},
fail: function (res) { },//接口调用失败的回调函数
complete: function (res) { },//接口调用结束的回调函数(调用成功、失败都会执行)
})
return false;
}
后续更新中.....