2017-09-15 48 views
0

我有一個JavaScript代碼whch只驗證密碼的整數。我想要的只是開發「var exp」的代碼,密碼必須是大寫字母,小寫字母,數字字符和特殊字符的組合。Javascript密碼長複雜驗證

HTML代碼工作完美..

//Empty Validation// 
 
function isEmpty(elemValue, field) { 
 
    if (elemValue == "" || elemValue == null) { 
 
    alert(field + " must not be empty!"); 
 
    return true; 
 
    } else { 
 
    return false; 
 
    } 
 
} 
 

 
//empty validation end// 
 

 
//password validation//   
 
function pwValidation(elemValue) { 
 
    if (!isEmpty(elemValue, "Password")) { 
 
    if (elemValue.length >= 8) { 
 
     var exp1 = /^[0-9]+$/; 
 

 
     if (elemValue.match(exp1)) { 
 
     return true; 
 
     } else { 
 
     alert("Password must contain Upper, lower case, number and at least a special character"); 
 
     return false; 
 
     } 
 
    } else { 
 
     alert("Password must at least 8 characters!"); 
 
    } 
 
    } else { 
 
    return false; 
 
    } 
 
} 
 
//password validation end//

回答

0

看看this回答前一個問題關於密碼驗證。至於說,它更容易改變你的正則表達式爲無效的密碼,而不是有效的,所以你應該輪翻轉你的邏輯,是這樣的:

//Empty Validation// 
 
function isEmpty(elemValue, field) { 
 
    if (elemValue == "" || elemValue == null) { 
 
    alert(field + " must not be empty!"); 
 
    return true; 
 
    } else { 
 
    return false; 
 
    } 
 
} 
 

 
//empty validation end// 
 

 

 
//password validation//   
 
function pwValidation(elemValue) { 
 
    if (!isEmpty(elemValue, "Password")) { 
 
    if (elemValue.length >= 8) { 
 
     var exp1 = /^(.{0,7}|[^0-9]*|[^A-Z]*|[^a-z]*|[a-zA-Z0-9]*)$/; 
 

 
     if (elemValue.match(exp1)) { 
 
     alert("Password must contain Upper, lower case, number and at least a special character"); 
 
     return false; 
 
     } else { 
 
     return true; 
 
     } 
 
    } else { 
 
     alert("Password must at least 8 characters!"); 
 
    } 
 
    } else { 
 
    return false; 
 
    } 
 
} 
 
//password validation end//