要求:长度为6-10,包含至少1个大写字母,至少1个小写字母,至少1个数字,不能含有中文字符、不能含有除字母及数字以外的英文字符
伊谢尔伦2017-04-11 11:54:05
var reg = /^(?![a-zA-Z]{6,10}$)(?![a-z0-9]{6,10}$)(?![0-9A-Z]{6,10}$)[a-zA-Z0-9]{6,10}$/;
console.log(reg.test('12abC'));
console.log(reg.test('aaaaaA'));
console.log(reg.test('aaaaa1'));
console.log(reg.test('aaa1A中'));
console.log(reg.test('aaa1A*'));
// 全 false
或者用相反的思路:
var reg2 = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[a-zA-Z0-9]{6,10}$/
大家讲道理2017-04-11 11:54:05
分步截取,并与运算符搭配使用
commit.onclick=function (){
var AZpass = /[A-Z]/.test(demo.value);
var azpass = /[a-z]/.test(demo.value);
var dpass = /\d/.test(demo.value);
var wordpass = /[^\w]/.test(demo.value);
var spacepass = /\s/.test(demo.value);
var lengthpass = /^\w{6,10}$/.test(demo.value); //字符在6至10之间
var pass = AZpass && azpass && dpass && (!wordpass) && (!spacepass) && lengthpass;
console.log(lengthpass);
if(!pass){
console.log("The infomation you entered is not compliant");
}else{
console.log("The infomation you entered is validated successfully");
};
};