2014-11-25 47 views
1

嘿,我有一個密碼驗證器,我有很多問題正在處理中,其相當冗長,我認爲可以縮短並儘可能簡化。將這兩個函數合併爲一個

有人可以幫助我簡化它。我在談論checkValidPassword()函數。

function check(input) { 
    if (input.value != document.getElementById('password').value) { 
     input.setCustomValidity('Password Must be Matching.'); 
    } else { 
     // input is valid -- reset the error message 
     input.setCustomValidity(''); 
     // check the length of the password 
     checkValidPassword(input); 
    } 
} 


function checkValidPassword(input) { 
    var password = document.getElementById('password'); 
    var confirm_password = document.getElementById('confirm password'); 
    if (password.value.length < 8) { 
     password.setCustomValidity('Password must contain at least 8 characters!'); 
    } else { 
     var re = /[0-9]/; 
     if (!re.test(password.value)) { 
      password.setCustomValidity('password must contain at least one number (0-9)!'); 
     } else { 
      password.setCustomValidity(""); 
     } 
    } 
} 

而即時通訊嘗試實現一種方式,用戶必須包含至少一個數字也。我在想

str.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/) 

我將包括在如果與$$ statment象徵並檢查字符

if(password.value.length < 8 && str.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8,})$/)) { 

回答

1

這基本上是一個代碼審查的問題,但確定...我已經重寫你的函數是這樣的:

function checkPassword() { 

    var password = document.getElementById('password'); 
    var confirm_password = document.getElementById('confirm password'); 

    if (password.value != confirm_password.value) { 
     password.setCustomValidity('Password Must be Matching.'); 
     return false; 
    } 

    if(password.value.length < 8) { 
     password.setCustomValidity('Password must contain at least 8 characters!'); 
     return false; 
    } 

    if(!/[0-9]/.test(password.value)) { 
     password.setCustomValidity('password must contain at least one number (0-9)!'); 
     return false; 
    } 

    return true; 
} 

基本上,逐個檢查每個條件,如果失敗立即返回,從而避免了額外的縮進(「提前退出」)。這有點冗長,但比怪物正則表達式更具可讀性,特別是如果你不確定它是什麼。

+1

感謝@georg,目前即時通訊使用** **我會保持oninput(checkthis)函數嗎?或將其更改爲** checkPassword()**? – xtrman 2014-11-25 10:12:17

+1

只是'checkPassword'會好的。 – georg 2014-11-25 10:14:00

+1

感謝您的幫助,但它似乎並沒有在這裏工作是一個jsf:http://jsfiddle.net/xb4u0453/我正確地調用它。 – xtrman 2014-11-25 10:20:46

1

我設法弄明白了,我把它們結合起來,只是把其他東西放在一起。

function ValidatePassword(pass, confirm_pass) { 
    if (pass.value != confirm_pass.value || pass.value == "" || confirm_pass.value == "") { 
     confirm_pass.setCustomValidity("the Passwords do not match"); 
     pass.setCustomValidity("the Passwords do not match"); 
    } else { 
     if(pass.value.match(/(?=^.{8,30}$)([a-zA-Z]+[0-9])$/)) { 

      pass.setCustomValidity(""); 
      confirm_pass.setCustomValidity(""); 


     } else { 
      pass.setCustomValidity("the password doesnt have numbers"); 
      confirm_pass.setCustomValidity("the password doesnt have numbers"); 
     } 
    } 
} 

下面是我編的外觀形式,如:

<form> 

     password 
     <input id="pass" type="password" required="" placeholder="Password" /> 
     <br> confirm 
     <input id="confirm_pass" type="password" required="" placeholder="confirm" onfocus="ValidatePassword(document.getElementById('pass'), this);" oninput="ValidatePassword(document.getElementById('pass'), this);" /> 
     <br> username : 
     <input id="username" required="" type="text"> 
     <br> 
     <button class="btnform" name="register" type="submit">Complete Registration</button> 
</form>