我正在使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/来验证允许用户更改其现有设置的表单。该表单有一个密码字段和一个确认密码的字段。为了保持密码不变,我指示用户将其留空(也许是一些更直观的方法?),并且仅当用户更改他/她的密码时才会显示确认密码字段。
它工作得很好,但有两个问题。首先,如果您在密码字段中输入内容并按 Tab,则不会转到确认密码字段,而是转到之后的下一个字段。其次,如果您在密码字段中输入内容并按回车,则在检查确认密码之前提交表单。出于某种原因,使用 jsfiddle 演示http://jsfiddle.net/4htKu/2/并没有体现出第二个缺陷,但它在第二个相同的演示http://tapmeister.com/test/validatePassword.html上确实存在。下面的代码与前面提到的两个演示相同。我相信问题与被隐藏的确认密码密码有关,但不确定。我需要做什么来解决这两个问题?谢谢
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>jQuery validation plug-in - main demo</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
$("#confirm_password").parent().hide();
$("#password").val('').
blur(function() {
$t=$(this);
if($.trim($t.val())!="" && !$t.hasClass('error')) {$("#confirm_password").parent().show();}
else if($.trim($t.val())=="") {$("#confirm_password").val('').parent().hide();}
});
// validate signup form on keyup and submit
$("#signupForm").validate({
rules: {
firstname: {required: true,minlength: 2},
lastname: {required: true,minlength: 2},
username: {required: true,minlength: 2},
password: {required: true,minlength: 2},
confirm_password: {required: true,minlength: 2, equalTo: "#password"}
},
messages: {},
submitHandler: function(form) {alert("submitted");}
});
});
</script>
</head>
<body>
<form class="cmxform" id="signupForm" method="get" action="">
<fieldset>
<legend>Validating a complete form</legend>
<p>
<label for="firstname">Firstname*</label>
<input id="firstname" name="firstname" />
</p>
<p>
<label for="lastname">Lastname*</label>
<input id="lastname" name="lastname" />
</p>
<p>
<label for="username">Username*</label>
<input id="username" name="username" />
</p>
<p>
<label for="password">Password (Leave blank to remain unchanged)</label>
<input id="password" name="password" type="password" />
</p>
<p>
<label for="confirm_password">Confirm password</label>
<input id="confirm_password" name="confirm_password" type="password" />
</p>
<p>
<label for="other">Other Non-required</label>
<input id="other" name="other" />
</p>
<p>
<input class="submit" type="submit" value="Submit" />
</p>
</fieldset>
</form>
</body>
</html>