js验证手机号、密码、短信验证码代码工具类

 更新时间:2020年06月24日 16:02:53   作者:Joker_Ye  
这篇文章主要介绍了js验证手机号、密码、短信验证码代码工具类,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了js验证手机号、密码、短信验证码的代码工具类,供大家参考,具体内容如下

代码工具类

/** 
 * 参数较验 
 * 
 * */ 
var verification = { 
 stop : false, //倒计时 
 //验证手机号 
 phone : function (tel, id) { 
  if ("" == tel || !tel) { 
   mui.toast('手机号不可以为空!'); 
  } else { 
   var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/; 
   //验证规则 
   if (reg.test(tel)) return true; 
   mui.toast("手机号错误!"); 
  } 
  document.getElementById(id).focus(); 
  return false; 
 }, 
 
 //验证密码(密码只能由数字和字母组成) 
 password : function (w, id) { 
  if ("" == w || !w) { 
   mui.toast('请输入密码!'); 
  } else if (w.length < 6) { 
   mui.toast('密码至少大于等于6位!'); 
  } else if (w.length > 20) { 
   mui.toast('密码不能超过20位!'); 
  } else if (w) { 
   var reg = /^[0-9a-zA-Z]+$/; 
   if (reg.test(w)) return true; 
   mui.toast("密码只能由数字和字母组成"); 
  } 
  document.getElementById(id).focus(); 
  return false; 
 }, 
 
 //验证码倒计时 
 code : function (tel, btn, type) { 
  var that = this, 
   tel = $.trim(tel); 
  if (!this.phone(tel, 'userTel')) return false; 
  if (true == that.stop) return false; //防止重复点击 
  that.stop = true; 
 
  var btn = $("#"+btn); 
  btn.attr("disabled", true).text("正在发送"); 
  var _no = 60; 
  var time = setInterval(function () { 
   _no--; 
   btn.text(_no + "秒后重发"); 
   if (_no == 0) { 
    //btn.attr("disabled", false).text("获取验证码"); 
    btn.removeAttr('disabled').text("重新获取验证码"); 
    that.stop = false; 
    _no = 60; 
    clearInterval(time); 
   } 
  }, 1000); 
 
  var url = "/Home/User/sendVerifyCode.html"; 
  $.post(url, { 
   toNumber: tel, 
   type:type 
  }, function (result) { 
   mui.toast(result.info); 
   if (200 != result.status) { 
    btn.removeAttr('disabled').text("获取验证码"); 
    that.stop = false; 
    _no = 60; 
    clearInterval(time); 
   } 
  }, 'json'); 
 } 
}; 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

最新评论