正则表达式验证不能按预期工作

时间:2014-04-24 09:32:53

标签: javascript regex

var validatePassword = function(password) {

    var upper = new RegExp(/^(?=.*[A-Z]).+$/),
    lower = new RegExp(/^(?=.*[a-z]).+$/),
    symbols = new RegExp(/^(?=.*(_|[^\w])).+$/),
    numbers = new RegExp(/^(?=.*\d).+$/);

    if((upper.test(password) && lower.test(password)) || (upper.test(password) && numbers.test(password)) || (upper.test(password) && symbols.test(password)) || (lower.test(password) && numbers.test(password)) || (lower.test(password) && symbols.test(password)))
    {
        return true;
    }
    else
    {
        alert('Invalid password')
    }
}
validatePassword('Foo1!');
});

只有在输入任何值时才返回true,如果我只输入一个,则返回真值

3 个答案:

答案 0 :(得分:1)

你应该这样做:

var upper = new RegExp(/^(?=.*[A-Z]).+$/),
    lower = new RegExp(/^(?=.*[a-z]).+$/),
    symbols = new RegExp(/^(?=.*(_|[^\w])).+$/),
    numbers = new RegExp(/^(?=.*\d).+$/);

为:

var upper = /^(?=.*[A-Z]).+$/,
    lower = /^(?=.*[a-z]).+$/,
    symbols = /^(?=.*(_|[^\w])).+$/,
    numbers = /^(?=.*\d).+$/;

你的正则表达式已经是一个正则表达式了。您不需要使用new RegExp("patter","flag");

答案 1 :(得分:0)

让我们先简化逻辑

var upper = new RegExp(/^(?=.*[A-Z]).+$/),
    lower = new RegExp(/^(?=.*[a-z]).+$/),
    symbols = new RegExp(/^(?=.*(_|[^\w])).+$/),
    numbers = new RegExp(/^(?=.*\d).+$/);

以上可以简化

var upper = /[A-Z]/,       // means it contains uppercase
    lower = /[a-z]/,       // means it contains lowercase
    symbols = /[\W_]/,     // means it contains non-word character or underscore
    numbers = /\d/;        // means it contains number

也在这里

// means must have (upper AND lower) OR ((upper OR lower) AND (symbol OR number))
if( 
    (upper.test(password) && lower.test(password)) ||
    (upper.test(password) && numbers.test(password)) ||
    (upper.test(password) && symbols.test(password)) ||
    (lower.test(password) && numbers.test(password)) ||
    (lower.test(password) && symbols.test(password))
  )

以上可以简化

// means must have (upper AND lower) OR ((upper OR lower) AND (symbol OR number))
if(
    (upper.test(password) && lower.test(password)) ||
    ((upper.test(password) || lower.test(password)) &&
    (symbols.test(password) || symbols.test(password)))
  )

因为它可以是(上限和下限)

upper.test(password) && lower.test(password)

可以使用

进行测试
var upandlow = /[a-z][A-Z]|[A-Z][a-z]/;

因为它必须有(上或下),所以也可以一步检查

var upperlower = /[a-zA-Z]/;   // means it contains uppercase OR lowercase

因为它必须有(符号OR编号),所以也可以一步检查

var symbolsnumbers = /[\d\W_]/; // means it contains number OR non-word OR underscore

现在可以简化整个功能

var validatePassword = function(password) {

  var upandlow = /[a-z][A-Z]|[A-Z][a-z]/,
      upperlower = /[a-zA-Z]/, 
      symbolsnumbers = /[\d\W_]/;

  if ( 
    upandlow.test(password) ||
    (upperlower.test(password) &&  symbolsnumbers.test(password))) {
      console.log('true');
      return true;
  }
  else {
      //alert('Invalid password');
      console.log('Invalid');
  }
}


validatePassword('Foo!');

以下是fiddle

祝你好运 :)

答案 2 :(得分:-1)

根据您的评论,我已更新了我的回答:

密码需要在least two之后与4 conditions匹配,8 characterstrue

var validatePassword = function(password) {

    var numero = 0;
//check if a digit is present    
if(/(?=.*?[\d]).*$/.test(password)) {
     numero++;
}
//check if a uppercase letter is present  
if(/(?=.*?[A-Z]).*$/.test(password)) {
        numero++;
}
//check if a lowercase letter is present
if(/(?=.*?[a-z]).*$/.test(password)) {
         numero++;
}
//check if a special character is present
if(/(?=.*?(_|[^\w])).*$/.test(password)) {
         numero++;
}
//check if at least 8 characters are present        
if (/^(?=.{8,}).*$/.test(password)) {
    var length = true;
} 

if( numero>=2 && length){

        alert('Valid password');
        console.log(numero);
        return true;
    }
    else
    {
        alert('Invalid password');
        console.log(numero);
    }

}
validatePassword('foo4ogD!');

http://jsfiddle.net/tuga/S57Bb/4/