js 常备校验工具方法

7 篇文章 0 订阅
1 篇文章 0 订阅

一 .字符串替换

  /**
 * 字符串替换
 * @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;
    } 


 后续更新中.....

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值