一 .字符串替換
/**
* 字符串替換
* @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;
}
後續更新中.....