亲宝软件园·资讯

展开

Vue手机号正则匹配

HqL丶1024 人气:0

一丶手机号

(1)正则匹配

if (!/^1[3|4|5|6|7|8|9]\d{9}$/.test(13807999985)) {
    console.log(resultPhone);//138****9985

(2)substr截取

    var phone = "17610603529";
    var mphone = phone.substr(0, 3) + "****" + phone.substr(7);
    // 如果用 Es6 模板字符串的话,可以不用+号做拼接
    var phone = "17610603529";
    var mphone = `${phone.substr(0, 3)}****${phone.substr(7)}`;
    console.log(mphone);

二丶姓名

(1)substr 截取

activated() {
    console.log(this.formatName("王八肚")); // 王**
  },
  methods: {
    formatName(name) {
      let newStr;
      if (name.length === 2) {
        newStr = name.substr(0, 1) + "*"; // 通过substr截取字符串从第0位开始截取,截取1个
      } else if (name.length > 2) {
        // 当名字大于2位时
        let char = "";
        for (let i = 0, len = name.length - 2; i < len; i++) {
          // 循环遍历字符串
          char += "*";
        }
        newStr = name.substr(0, 1) + char + name.substr(-1, 1);
      } else {
        newStr = name;
      }
      return newStr;
    },
  },

加载全部内容

相关教程
猜你喜欢
用户评论