微信小程序自動識別姓名電話地址


 

舉例數據:

var str = "姓名:懸劍電話:18686868866地址:北京市五道口職業技術學院";

var str2 = "懸劍18686868866北京市五道口職業技術學院";

 

  changtext: function(e) {
    var that = this,
      text = e.detail.value,
      text = text.replace(/(^\s*)|(\s*$)/g, "");
    console.log(text);

    if (text == '') {
      that.setData({
        u_name: '',
        u_phone: '',
        u_address: ''
      })
      return;
    }

    var regx = /(1[3|4|5|7|8][\d]{9}|0[\d]{2,3}-[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})/g;
    var phone_num = text.match(regx);
    if (phone_num != null) {
      var phone = text.indexOf(phone_num[0]);
    }
    var name = text.indexOf("姓名:");
    if (name >= 0) {
      var phone = text.indexOf("電話:"),
        address = text.indexOf("地址:");
      var u_name = text.substring(name + 3, phone),
        u_phone = text.substring(phone + 3, address),
        u_address = text.substring(address + 3, text.length);
      that.setData({
        u_name: u_name,
        u_phone: u_phone,
        u_address: u_address
      })
    } else if (phone >= 0) {
      var u_name = text.substring(0, phone),
        u_phone = text.substring(phone, phone + 11),
        u_address = text.substring(phone + 11, text.length);
      that.setData({
        u_name: u_name,
        u_phone: u_phone,
        u_address: u_address
      })
    } else {
      that.setData({
        u_name: '',
        u_phone: '',
        u_address: ''
      })
      return;
    }

輸出:

var u_name = "懸劍",
    u_phone = "18686868866",
    u_address = "北京市五道口職業技術學院";


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM