面試 js驗證 兩次密碼是否一致(程式碼)
<%@taglib prefix="s" uri="/struts-tags" %> /* 用到了struts,注意引用該標籤在jsp介面*/
<body>
<s:form action="confirmMofifyPassword.action" method="get">
<s:password name="companyPassword" label="密碼"></s:password>
<s:password name="newpassword" label="新密碼" id="newpassword"></s:password>
<s:password name="confirmNewPassword" label="確認新密碼" id="confirmNewPassword" ></s:password>
<span id="tishi"></span>
<s:submit value="修改" onclick="return validate()">
</s:submit>
</s:form>
<script type="text/javascript">
function validate(){
var word1= document.getElementById("newpassword").value;
var word2 = document.getElementById("confirmNewPassword").value;
if(word1 != word2){
window.alert("兩次輸入的新密碼不一致!");
confirmNewPassword.focus();
return false;
}
return true;
}
</script>
</body>