2011-12-16 33 views
0

我想記住如何做到這一點。我需要創建兩個密碼字段並且密碼必須匹配。通過按下按鈕來檢查它們是否匹配。他們必須有一個大寫字母和一個數字,長度爲4個字符。如果密碼通過了所有這些,你就可以進入另一個頁面。無法使用Javascript。有一段時間沒有這樣做

到目前爲止,我

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 

<html> 
<head> 
<script type="text/javascript"> 
function password() 
{ 
var pwd1 = document.getElementsByName("pwd1").value; 
var pwd2 = document.getElementsByName("pwd2").value; 

if(pwd1 == pwd2) 
{ 
window.location.href = '...'; 
} 
else 
{ 
Alert("Passwords do not match or your password is not longer than 4 characters"); 
} 
} 

</script> 
</head> 
<body> 

<input type="password" name="pwd1"/> 
<input type="password" name="pwd2"/> 
<input type="button" onClick="password();" value="Submit" /> 


</body> 
</html> 

回答

2

錯誤條件...

(pwd1 != pwd2 || pwd1.length >= 4 || ! pwd1.match(/\d/) || ! pwd1.match(/[A-Z]/)) 

此外,alert()沒有資本,你的元素的選擇需要下標([0])的第一。

jsFiddle

+0

@ZacharyBurt:哎呦,糾正:) – alex 2011-12-16 02:12:25

+0

所以這應該是到位的「pwd1 == pwd2」中的if語句? – 2011-12-16 02:19:20

2

好吧,那麼你的密碼的功能應該是這個樣子:

function password() { 
    var pwd1 = document.getElementsByName("pwd1")[0].value 
    , pwd2 = document.getElementsByName("pwd2")[0].value 
    , rules = /(?=.*\d)(?=.*[A-Z])/; 

    if(pwd1 === pwd2 && pwd1.length >= 4) { 
    if(rules.test(pwd1)) { 
     // redirect them now 
     // window.location.href = ... 
    } else { 
     alert('Password must contain at least 1 Capital letter and 1 digit'); 
    } 
    } 
    else{ 
    alert('Passwords must match and be at least 4 characters'); 
    } 
} 
相關問題