2017-02-14 171 views
-2

我需要包含密码验证是至少8个字符包括一个特殊字符和至少一个大写字母。在javascript中的密码验证

我已经尝试了这个脚本:

$(document).on('keyup', '.passworduser', function() { 
 
    var txtInput = $(this).val(); 
 
    var regPass = "^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#[email protected]$%^&*-_]).{8,}$"; 
 
    var color = "Red"; 
 
    if (txtInput.length > 7) { 
 
    if (!txtInput.match(regPass)) { 
 
     $('#password_strength').html("Include a special character and at least one capital letter"); 
 
     $("#password_strength").css("color", color); 
 

 
    } else { 
 
     $('#password_strength').html(""); 
 

 
    } 
 
    } else { 
 
    $('#password_strength').html("Password to be a minimum of 8 characters"); 
 
    $("#password_strength").css("color", color); 
 

 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<input type="text" class="passworduser"> 
 
<div id="password_strength"></div>

对于一些表达它的工作,对一些人来说并非如此。

+1

*对于某些表达它正在工作,对于一些它不是。* =>你能澄清吗?此外,你会惊讶什么'[* -_]'做... –

+0

@托马斯:包括下划线和减号,如果用户想添加这就是我已经使用。 – heema

+0

查看Tushar关于char类中连字符语义的回答。请参阅[demo](https://regex101.com/r/VX08iT/1) –

回答

1

regPass字符串。它应该包裹在反斜杠上以制作正则表达式或将字符串传递给RegExp构造函数。

var regPass = /^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#[email protected]$%^&*\-_]).{8,}$/; 
在字符类

OR

var regPass = new RegExp("^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#[email protected]$%^&*\\-_]).{8,}$"); 

此外,-(连字符)被用于选择字符范围。要从字面上匹配它,请将其转义或移动到字符类的开头或结尾。


下面是完整的代码有一些变化

// Can be moved outside 
var regex = /^(?=.*?[0-9])(?=.*?[A-Z])(?=.*?[#[email protected]$%^&*\-_]).{8,}$/; 

$(document).on('keyup', '.passworduser', function() { 
    var value = $(this).val(); 
    var color = "Red"; 

    if (value.length > 7) { 
     // Use RegExp.test instead of String.match 
     if (regex.test(value)) { 
      // Method chaining 
      $('#password_strength').html("Include a special character and at least one capital letter") 
       .css("color", color); 
     } else { 
      $('#password_strength').html(""); 
     } 
    } else { 
     $('#password_strength').html("Password to be a minimum of 8 characters") 
      .css("color", color); 
    } 
}); 
+0

它应该是'new RegExp(“^(?=。*?[0-9])(?=。*?[AZ] )(?=。*?[#?!@ $%^&* \\ -_])。{8,} $“' –

+0

@WiktorStribiżew谢谢指出。 – Tushar

0

保持您正则表达式简单。调试起来更容易。试试这个:

function testPassword(pw) { 
// for all special characters 
var reSpecial = /[#[email protected]$%^&*\-_\\\/]/; 
// for Capitals 
var reCap = /[A-Z]/; 

// just test both of them... 
return pw.match(reSpecial) != undefined && pw.match(reCap) != undefined; 
}; 

console.log(testPassword("asdf"));// false 
console.log(testPassword("asdf5"));//false 
console.log(testPassword("asdA"));//false 
console.log(testPassword("asdf%"));//false 
console.log(testPassword("asdfA%"));//true 


$(document).on('keyup', '.passworduser', function(){ 
    var color="Red"; 
    if(txtInput.length > 7){ 
    if(!testPassword(txtInput){ 
     $('#password_strength').html("Include a special character and at least one capital letter"); 
     $("#password_strength").css("color", color); 
    }else{ 
    $('#password_strength').html(""); 
    } 
    }else{ 
    $('#password_strength').html("Password to be a minimum of 8 characters");  
    $("#password_strength").css("color", color); 
    } 
}); 
0

请试试这个,我已经更新了你的代码,这里是下面的代码:

$(document).on('keyup', '.passworduser', function(){ 
    var txtInput = $(this).val(); 
    var color="Red"; 
    var uppercase = /[A-Z]/ ; 
    var lowercase = /[a-z]/ ; 
    var number = /[0-9]/ ; 
    var special = /[\W]{1,}/ ; 
    var pswd_length = txtInput.length < 8; 
    if(!uppercase.test(txtInput) || !lowercase.test(txtInput) || !number.test(txtInput) || !special.test(txtInput) || pswd_length) { 
     $('#password_strength').html('Password must be at least 8 characters. Password must contain at least one upper case letter, one lower case letter and one digit.'); 
     $("#password_strength").css("color", color); 
     //return false; 
    } 
    else { 
     $('#password_strength').html(""); 
    } 

}); 

我希望,这可能会对你有所帮助。