表单验证中的各类正则表达式

验证:!reg.test(value)

邮箱:

  1. reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i;  
reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/i;


不包含中文:

  1. reg = /.*[\u4e00-\u9fa5]+.*$/i;  
reg = /.*[\u4e00-\u9fa5]+.*$/i;

身份证号:

  1. // 验证身份证号码  
  2.     var city = {11:'北京',12:'天津',13:'河北',14:'山西',15:'内蒙古',21:'辽宁',22:'吉林',23:'黑龙江 ',31:'上海',32:'江苏',33:'浙江',34:'安徽',35:'福建',36:'江西',37:'山东',41:'河南',42:'湖北 ',43:'湖南',44:'广东',45:'广西',46:'海南',50:'重庆',51:'四川',52:'贵州',53:'云南',54:'西藏 ',61:'陕西',62:'甘肃',63:'青海',64:'宁夏',65:'新疆',71:'台湾',81:'香港',82:'澳门',91:'国外 '};  
  3.     function check_idcard(n)  
  4.     {  
  5.         var im = 0;  
  6.         var info, i, bd;  
  7.           
  8.         if(n.length != 18)  
  9.         {  
  10.             return false;  
  11.         }  
  12.         n = n.replace(/x$/i,"a");  
  13.         if(city[parseInt(n.substr(0,2))] == null)  
  14.         {  
  15.             return false;  
  16.         }  
  17.         bd = n.substr(6,4)+"-"+Number(n.substr(10,2))+"-"+Number(n.substr(12,2));  
  18.         var d = new Date(bd.replace(/-/g,"/"))  
  19.         if(bd != (d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))  
  20.         {  
  21.             return false;  
  22.         }  
  23.         for(i = 17; i >= 0; i--)  
  24.         {  
  25.             im += (Math.pow(2,i) % 11) * parseInt(n.charAt(17 - i),11)  
  26.         }  
  27.         if(im % 11 != 1)  
  28.         {  
  29.             return false;  
  30.         }  
  31.         return true;  
  32.     }  
// 验证身份证号码
	var city = {11:'北京',12:'天津',13:'河北',14:'山西',15:'内蒙古',21:'辽宁',22:'吉林',23:'黑龙江 ',31:'上海',32:'江苏',33:'浙江',34:'安徽',35:'福建',36:'江西',37:'山东',41:'河南',42:'湖北 ',43:'湖南',44:'广东',45:'广西',46:'海南',50:'重庆',51:'四川',52:'贵州',53:'云南',54:'西藏 ',61:'陕西',62:'甘肃',63:'青海',64:'宁夏',65:'新疆',71:'台湾',81:'香港',82:'澳门',91:'国外 '};
	function check_idcard(n)
	{
		var im = 0;
		var info, i, bd;
		
		if(n.length != 18)
		{
			return false;
		}
		n = n.replace(/x$/i,"a");
		if(city[parseInt(n.substr(0,2))] == null)
		{
			return false;
		}
		bd = n.substr(6,4)+"-"+Number(n.substr(10,2))+"-"+Number(n.substr(12,2));
		var d = new Date(bd.replace(/-/g,"/"))
		if(bd != (d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
		{
			return false;
		}
		for(i = 17; i >= 0; i--)
		{
			im += (Math.pow(2,i) % 11) * parseInt(n.charAt(17 - i),11)
		}
		if(im % 11 != 1)
		{
			return false;
		}
		return true;
	}

密码强度检测:

  1. function  ops_pwd_set(obj)  
  2. {  
  3.     var pwd = obj;  
  4.     if(typeof obj.value != 'undefined')  
  5.         pwd = obj.value;  
  6.       
  7.     if(pwd == '')  
  8.         return false;  
  9.   
  10.     var len = pwd.length;  
  11.       
  12.     if(pwd == '')  
  13.     {  
  14.         ops_show("密码不能为空",true);  
  15.         return false;  
  16.     }  
  17.       
  18.     //中文  
  19.     if(/[\u4e00-\u9fa5]+/.test(pwd)){  
  20.         ops_show("密码不能含有中文",true);  
  21.         return false;  
  22.     }  
  23.     //空白  
  24.     var tmppwd = pwd.replace(/\s+/g,"");  
  25.     if(tmppwd != pwd){  
  26.         ops_show("密码不能含有空白字符",true);  
  27.         return false;  
  28.     }  
  29.   
  30.     //长度  
  31.     if( len < 6 || len > 20){  
  32.         ops_show("密码长度需在6-20位之间",true);  
  33.         return false;  
  34.     }  
  35.   
  36.     //正则表达式  
  37.     var is_all_num = /^\d+$/.test(pwd) ;  
  38.     var have_num = /\d/.test(pwd) ;  
  39.     var is_all_abc = /^[a-zA-Z]+$/.test(pwd) ;  
  40.     var have_abc = /[a-zA-Z]/.test(pwd) ;  
  41.     var have_strong = /[^a-zA-Z0-9]/.test(pwd) ;  
  42.     var is_very_strong = pwd.split(/[^a-zA-Z_0-9]/).length > 2 ;  
  43.     /** 
  44.      * 禁用: 
  45.      *  1)全为数字且少于8位 
  46.      *  2)密码字符全部相同 
  47.     */  
  48.     var disable_msg = "此密码安全性太弱,不可用" ;  
  49.     if(is_all_num && len < 8){  
  50.         ops_show(disable_msg,true);  
  51.         return false;  
  52.     }  
  53.     var i=0;  
  54.     var apwd = pwd.split('');  
  55.     for(i=1; i < len ; i ++)  
  56.         if(apwd[i] != apwd[0])  
  57.             break;  
  58.     if(i >= len){  
  59.         ops_show(disable_msg,true);  
  60.         return false;  
  61.     }  
  62.   
  63.     /** 
  64.      * 极强: 
  65.      *  1)数字与字母与其它可打印字符(大于2位)且大于等于8位 
  66.      */  
  67.     if( (have_num && have_abc && is_very_strong && len >= 8) ){  
  68.         ops_show('<font color="#00cc00">极强</font>');  
  69.         return true;  
  70.     }  
  71.   
  72.     /** 
  73.      * 强: 
  74.      *  1)数字与字母与其它可打印字符且大于等于8位  
  75.      *  2)数字与字母与其它可打印字符(大于2位)且小于8位 
  76.      *  3)两两组合大于等于8位 
  77.      */   
  78.     if( (have_num && have_abc && have_strong && len >= 8)   
  79.             || (have_num && have_abc && is_very_strong && len < 8)   
  80.             || ( ( (have_num && have_strong) || (have_abc && have_strong) ) && len >= 8 ) ){  
  81.         ops_show('<font color="#00cc00">强</font>');  
  82.         return true;  
  83.     }  
  84.   
  85.     /** 
  86.      * 中: 
  87.      *  1)数字与字母混合且大于等于8位 
  88.      *  2)数字与字母与其它可打印字符且小于8位 
  89.      *  3)两两组合 
  90.      */  
  91.     if( (have_num && have_abc && len >= 8) || (have_num && have_abc && have_strong && len < 8)   
  92.             || ( (have_num && have_strong) || (have_abc && have_strong) ) ){  
  93.         ops_show('<font color="#ff9900">密码强度中</font>');  
  94.         return true;  
  95.     }  
  96.       
  97.     /** 
  98.      * 弱: 
  99.      *  1)全部为数字或字母 
  100.      *  2)数字与字母混合且少于8位 
  101.      */  
  102.     if( (is_all_num || is_all_abc) || (have_num && have_abc && len < 8) ){  
  103.         ops_show('<font color="#ff0000">密码强度弱</font>');  
  104.         return true;  
  105.     }  
  106.     //其它  
  107.     ops_show('<font color="#ff0000">密码强度一般</font>');  
  108.     return true;  
  109. }  
function  ops_pwd_set(obj)
{
	var pwd = obj;
	if(typeof obj.value != 'undefined')
		pwd = obj.value;
	
	if(pwd == '')
		return false;

	var len = pwd.length;
	
	if(pwd == '')
	{
		ops_show("密码不能为空",true);
		return false;
	}
	
	//中文
	if(/[\u4e00-\u9fa5]+/.test(pwd)){
		ops_show("密码不能含有中文",true);
		return false;
	}
	//空白
	var tmppwd = pwd.replace(/\s+/g,"");
	if(tmppwd != pwd){
		ops_show("密码不能含有空白字符",true);
		return false;
	}

	//长度
	if( len < 6 || len > 20){
		ops_show("密码长度需在6-20位之间",true);
		return false;
	}

	//正则表达式
	var is_all_num = /^\d+$/.test(pwd) ;
	var have_num = /\d/.test(pwd) ;
	var is_all_abc = /^[a-zA-Z]+$/.test(pwd) ;
	var have_abc = /[a-zA-Z]/.test(pwd) ;
	var	have_strong = /[^a-zA-Z0-9]/.test(pwd) ;
	var is_very_strong = pwd.split(/[^a-zA-Z_0-9]/).length > 2 ;
	/**
	 * 禁用:
	 * 	1)全为数字且少于8位
	 * 	2)密码字符全部相同
	*/
	var disable_msg = "此密码安全性太弱,不可用" ;
	if(is_all_num && len < 8){
		ops_show(disable_msg,true);
		return false;
	}
	var i=0;
	var apwd = pwd.split('');
	for(i=1; i < len ; i ++)
		if(apwd[i] != apwd[0])
			break;
	if(i >= len){
		ops_show(disable_msg,true);
		return false;
	}

	/**
	 * 极强:
	 *  1)数字与字母与其它可打印字符(大于2位)且大于等于8位
	 */
	if( (have_num && have_abc && is_very_strong && len >= 8) ){
		ops_show('<font color="#00cc00">极强</font>');
		return true;
	}

	/**
	 * 强:
	 *  1)数字与字母与其它可打印字符且大于等于8位 
	 *  2)数字与字母与其它可打印字符(大于2位)且小于8位
	 *  3)两两组合大于等于8位
	 */ 
	if( (have_num && have_abc && have_strong && len >= 8) 
			|| (have_num && have_abc && is_very_strong && len < 8) 
			|| ( ( (have_num && have_strong) || (have_abc && have_strong) ) && len >= 8 ) ){
		ops_show('<font color="#00cc00">强</font>');
		return true;
	}

	/**
	 * 中:
	 *  1)数字与字母混合且大于等于8位
	 *  2)数字与字母与其它可打印字符且小于8位
	 *  3)两两组合
	 */
	if( (have_num && have_abc && len >= 8) || (have_num && have_abc && have_strong && len < 8) 
			|| ( (have_num && have_strong) || (have_abc && have_strong) ) ){
		ops_show('<font color="#ff9900">密码强度中</font>');
		return true;
	}
	
	/**
	 * 弱:
	 *  1)全部为数字或字母
	 *  2)数字与字母混合且少于8位
	 */
	if( (is_all_num || is_all_abc) || (have_num && have_abc && len < 8) ){
		ops_show('<font color="#ff0000">密码强度弱</font>');
		return true;
	}
	//其它
	ops_show('<font color="#ff0000">密码强度一般</font>');
	return true;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值