我試圖確認密碼強度是強還是弱,并且密碼是強的,當我提交它時應該有像“你有強密碼”這樣的警報訊息,當它的弱時是“無效密碼”
這就是我現在的樣子。
function checkPasswordStrength() {
var passwordStrength = false;
var number = /([0-9])/;
var alphabets = /([a-zA-Z])/;
var special_characters = /([~,!,@,#,$,%,^,&,*,-,_, ,=,?,>,<])/;
if ($('#password').val().length < 8) {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('weak-password');
$('#password-strength-status').html("Weak (should be atleast 8 characters.)");
} else {
if ($('#password').val().match(number) && $('#password').val().match(alphabets) && $('#password').val().match(special_characters)) {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('strong-password');
$('#password-strength-status').html("Strong");
return passwordStrength = true;
} else {
$('#password-strength-status').removeClass();
$('#password-strength-status').addClass('medium-password');
$('#password-strength-status').html("Medium (should include alphabets, numbers and special characters.)");
}
}
}
$('#btn-submit').click(function () {
if (passwordStrength == false) {
alert("INVALID PASSWORD");
} else {
alert("You have Strong PASSWORD");
}
</script>
它僅用于教育目的,我剛剛開始 jquery .. 提前謝謝你..
uj5u.com熱心網友回復:
看起來變數范圍不正確。var passwordStrength應該放在checkPasswordStrength函式之外。
var passwordStrength
function checkPasswordStrength() {
....
uj5u.com熱心網友回復:
您需要呼叫該函式,而不僅僅是檢查您的變數。所以寧愿做
$('#btn-submit').click(function () {
if (checkPasswordStrength() === false) {
代替
$('#btn-submit').click(function () {
if (passwordStrength == false) {
然后,return passwordStrength = true;您不應該只是在函式的最后passwordStrength = true添加 a return passwordStrength,這樣它就會回傳 false 或 true。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/405462.html
標籤:
上一篇:如何將滑鼠懸停在下拉選單上?
