使用jQuery和java验证手机号码或者电话号码

来源:互联网 发布:逻辑学悖论破解知乎 编辑:程序博客网 时间:2024/05/16 08:22

我们会经常使用到验证手机号或者电话的操作,下面就总结前端使用jQuery后端使用java的验证方法:

知识点一:前段Jquery验证:

    var phone = $("#id名称").val().trim();    function validatePhone(phone){        //验证规则        var isMobile = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1})|(14[0-9]{1}))+\d{8})$/;          var isPhone = /^(?:(?:0\d{2,3})-)?(?:\d{7,8})(-(?:\d{3,}))?$/;         //如果为1开头则验证手机号码          if (mobile.substring(0, 1) == 1) {              if (!isMobile.exec(mobile) && mobile.length != 11) {                  //错误提示信息                return false;              }          }          //如果为0开头则验证固定电话号码          else if (mobile.substring(0, 1) == 0) {              if (!isPhone.test(mobile)) {                 //错误提示信息                  return false;              }          }          //否则全部不通过          else {           //错误提示信息             return false;          }          return true;      }

知识点二:后台java验证:

 /**       * 手机号验证       * @author :zxy      * @since 2017年5月2日18:16:40      * @param  str       * @return 验证通过返回true       */       public static boolean isMobile(final String str) {           Pattern p = null;           Matcher m = null;           boolean b = false;           p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号           m = p.matcher(str);           b = m.matches();           return b;       }       /**       * 电话号码验证       * @author :zxy      * @since 2017年5月2日18:16:40      * @param  str       * @return 验证通过返回true       */       public static boolean isPhone(final String str) {           Pattern p1 = null, p2 = null;           Matcher m = null;           boolean b = false;           p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");  // 验证带区号的           p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$");         // 验证没有区号的           if (str.length() > 9) {              m = p1.matcher(str);              b = m.matches();           } else {               m = p2.matcher(str);              b = m.matches();           }           return b;       }  

以上文章参考了:
http://blog.csdn.net/make1828/article/details/40823693
另一个网址我忘记收藏了,对不住你要博主!

1 0
原创粉丝点击