8-16位密码,英文大小写,数字,特殊字符_~@#$^至少包含3种,且不能4位连续重复或者串联(aaaa,1234,abcd,fedc···)

如果是html的话可以直接使用:


	
         
	

	


在vue页面:

由于String.prototype.hashCode在vue中报错,暂时没找到解决办法,所以只能笨方法先顶上了

function checkPassWord(password) {
	let msg1 = '密码中不能包含连续4位及以上相同或串联,如包含abcd、DCBA、1234、aaaa、2222等'
	let msg2 = '密码需使用大写字母、小写字母、数字及特殊字符(_~@#$^)四种字符中至少三种的组合'
	let msg3 = '密码位数为8-16个字符,且区分大小写,支持字母(a-z,A-Z)、数字(0-9)及“_~@#$^”符号'
	let msg4 = 'succ'
	let msg5 = '密码不符合规范'
	// _~@#$^
	// let str = password.toLowerCase()
	// passArr.forEach(item=>{
	// 	if(str.indexOf(item)>-1){
	// 		return msg1
	// 	}
	// })
	// reg 校验4个重复 比如1111 aaaa
	const reg = /(\w)\1{3}/
	if(reg.test(password)){
		return msg5
	}
	
	if(password.length > 16 || password.length < 8){
		return msg5
	}
	let num = 0
	let reg1 = /[a-z]/
	let reg2 = /[A-Z]/
	let reg3 = /[0-9]/
	let reg4 = /[_~@#$^]/
	if(reg1.test(password)){
		num++
	}
	if(reg2.test(password)){
		num++
	}
	if(reg3.test(password)){
		num++
	}
	if(reg4.test(password)){
		num++
	}
	if(num<3){
		return msg5
	}
	if(isPasswordContinuous(password)){
		return msg5
	}
	return msg4
}

function hashCode(str){
	let hasCodeArr = [
		{label:'0',val:0},{label:'1',val:1},{label:'2',val:2},{label:'3',val:3},{label:'4',val:4},{label:'5',val:5},{label:'6',val:6},{label:'7',val:7},
		{label:'8',val:8},{label:'9',val:9},
		{label:'a',val:110},{label:'b',val:111},{label:'c',val:112},{label:'d',val:113},{label:'e',val:114},{label:'f',val:115},
		{label:'g',val:116},{label:'h',val:117},{label:'i',val:118},{label:'j',val:119},{label:'k',val:120},{label:'l',val:121},
		{label:'m',val:122},{label:'n',val:123},{label:'o',val:124},{label:'p',val:125},{label:'q',val:126},{label:'r',val:127},
		{label:'s',val:128},{label:'t',val:129},{label:'u',val:130},{label:'v',val:131},{label:'w',val:132},{label:'x',val:133},
		{label:'y',val:134},{label:'z',val:135},
		{label:'A',val:1110},{label:'B',val:1111},{label:'C',val:1112},{label:'D',val:1113},{label:'E',val:1114},{label:'F',val:1115},
		{label:'G',val:1116},{label:'H',val:1117},{label:'I',val:1118},{label:'J',val:1119},{label:'K',val:1120},{label:'L',val:1121},
		{label:'M',val:1122},{label:'N',val:1123},{label:'O',val:1124},{label:'P',val:1125},{label:'Q',val:1126},{label:'R',val:1127},
		{label:'S',val:1128},{label:'T',val:1129},{label:'U',val:1130},{label:'V',val:1131},{label:'W',val:1132},{label:'X',val:1133},
		{label:'Y',val:1134},{label:'Z',val:1135},
		{label:'_',val:2110},{label:'@',val:2111},{label:'#',val:2112},{label:'$',val:2113},{label:'^',val:2114},{label:'~',val:2115}
	]
	let val = 0
	hasCodeArr.forEach(item=>{
		if(item.label == str){
			val = item.val
			return
		}
	})
	return val
}
// String.prototype.hashCode = function(){
// 	//如果已经缓存了结果,直接返回
//     if(this.hashcode!==undefined){
//         return this.hashcode
//     }
//     let hashcode = 0
//     for(let i=0;i 2 || reverseCount > 2 || sameCount>2) break;
        }
        if(count > 2 || reverseCount > 2 || sameCount>2) return true;
        return false;
    }
function isPositiveContinuous(str1,str2) {
        if(hashCode(str2) - hashCode(str1) == 1) return true;
        return false;
    }
function isReverseContinuous(str1,str2) {
	if(hashCode(str2) - hashCode(str1) == -1) return true;
	return false;
}

正则校验数字、字母、大写字母、特殊字符至少含有三种:

转载两种正则

第一种:
/((^(?=.*[a-z])(?=.*[A-Z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[A-Z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[a-z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\da-zA-Z\W]{8,16}$))/

第二种:
/^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,16}$/

let password = '12345678Abc'
if (!(/((^(?=.*[a-z])(?=.*[A-Z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[A-Z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[a-z])(?=.*\W)[\da-zA-Z\W]{8,16}$)|(^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\da-zA-Z\W]{8,16}$))/.test(password))) {
      console.log('请输入8-16位字符,至少包含数字、大写字母、小写字母、特殊字符中的三种类型')
    } else {
      console.log('ok')
    }

你可能感兴趣的:(前端,小程序,css,javascript)