js正则表达式:验证邮箱格式、密码复杂度、手机号码、QQ号码

来源:互联网 发布:eve技能点算法 编辑:程序博客网 时间:2024/05/01 14:27
  1. $(function () {  
  2.             $("input[name='sub']").on("click"function () {  
  3.                 if (!isEmail($("input[name='email']").val())) {  
  4.                     $("span[name='email']").html("邮箱格式错误");  
  5.                     return false;  
  6.                 }  
  7.                 else {  
  8.                     $("span[name='email']").html("");  
  9.                 }  
  10.                 if (checkStrong($("input[name='password']").val()) < 3) {  
  11.                     $("span[name='password']").html("密码太过简单");  
  12.                     return false;  
  13.                 }  
  14.                 else {  
  15.                     $("span[name='password']").html("");  
  16.                 }  
  17.                 if (!isQQ($.trim($("input[name='qq']").val()))) {  
  18.                     $("span[name='qq']").html("请输入正确的QQ号码");  
  19.                     return false;  
  20.                 }  
  21.                 else {  
  22.                     $("span[name='qq']").html("");  
  23.                 }  
  24.                 if (!isPhone($.trim($("input[name='mnumber']").val()))) {  
  25.                     $("span[name='mnumber']").html("请输入正确的手机号码");  
  26.                     return false;  
  27.                 }  
  28.                 else {  
  29.                     $("span[name='mnumber']").html("");  
  30.                 }  
  31.                 return true;  
  32.             });  
  33.         });  
  34.         /** 
  35.         * 检查字符串是否为合法QQ号码 
  36.         * @param {String} 字符串 
  37.         * @return {bool} 是否为合法QQ号码 
  38.         */  
  39.         function isQQ(aQQ) {  
  40.             var bValidate = RegExp(/^[1-9][0-9]{4,9}$/).test(aQQ);  
  41.             if (bValidate) {  
  42.                 return true;  
  43.             }  
  44.             else  
  45.                 return false;  
  46.         }  
  47.         /** 
  48.         * 检查字符串是否为合法手机号码 
  49.         * @param {String} 字符串 
  50.         * @return {bool} 是否为合法手机号码 
  51.         */  
  52.         function isPhone(aPhone) {  
  53.             var bValidate = RegExp(/^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$/).test(aPhone);  
  54.             if (bValidate) {  
  55.                 return true;  
  56.             }  
  57.             else  
  58.                 return false;  
  59.         }  
  60.         /** 
  61.         * 检查字符串是否为合法email地址 
  62.         * @param {String} 字符串 
  63.         * @return {bool} 是否为合法email地址 
  64.         */  
  65.         function isEmail(aEmail) {  
  66.             var bValidate = RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(aEmail);  
  67.             if (bValidate) {  
  68.                 return true;  
  69.             }  
  70.             else  
  71.                 return false;  
  72.         }  
  73.         /** 
  74.         * 检查字符串是否是整数 
  75.         * @param {String} 字符串 
  76.         * @return {bool} 是否是整数 
  77.         */  
  78.         function isInteger(s) {  
  79.             var isInteger = RegExp(/^[0-9]+$/);  
  80.             return (isInteger.test(s));  
  81.         }  
  82.         /* 
  83.             判断字符类型 
  84.         */  
  85.         function CharMode(iN) {  
  86.             if (iN >= 48 && iN <= 57) //数字    
  87.                 return 1;  
  88.             if (iN >= 65 && iN <= 90) //大写字母    
  89.                 return 2;  
  90.             if (iN >= 97 && iN <= 122) //小写    
  91.                 return 4;  
  92.             else  
  93.                 return 8; //特殊字符    
  94.         }  
  95.         /* 
  96.             统计字符类型 
  97.         */  
  98.         function bitTotal(num) {  
  99.             modes = 0;  
  100.             for (i = 0; i < 4; i++) {  
  101.                 if (num & 1) modes++;  
  102.                 num >>>= 1;  
  103.             }  
  104.             return modes;  
  105.         }  
  106.         /* 
  107.             返回密码的强度级别 
  108.         */  
  109.         function checkStrong(sPW) {  
  110.             if (sPW.length <= 4)  
  111.                 return 0; //密码太短    
  112.             Modes = 0;  
  113.             for (i = 0; i < sPW.length; i++) {  
  114.                 //测试每一个字符的类别并统计一共有多少种模式.    
  115.                 Modes |= CharMode(sPW.charCodeAt(i));  
  116.             }  
  117.             return bitTotal(Modes);  
  118.         }
0 0