我正在查看以下问题:Password confirmation trouble with jQuery validation plugin
我在jsfiddle上复制了代码并对其进行了相应的编辑以使其在我的服务器上运行,但它似乎没有工作,我无法理解它!请帮助,谢谢!
这是我的代码。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
<script>
$.validator.setDefaults({
submitHandler: function() {
alert("submitted!");
}
});
// validate signup form on keyup and submit
$("form").validate({
rules: {
pass: {
required: true,
minlength: 5
},
pass2: {
required: true,
minlength: 5,
equalTo: "#pass"
}
},
messages: {
pass: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
pass2: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long",
equalTo: "Please enter the same password as above"
}
}
});
</script>
</head>
<body>
<form>
<table>
<tr>
<td>Password</td>
<td>
<label for="pass"></label>
<input id="pass" name="pass" type="password" value="" />
</td>
</tr>
<tr>
<td>Password confirm</td>
<td>
<label for="pass2"></label>
<input id="pass2" name="pass2" type="password" value="" />
</td>
</tr>
</table>
<input type="submit" name="save" value="Save" />
</form>
</body>
</html>
答案 0 :(得分:4)
尝试包装你的JS这个功能
$(function () {
$.validator.setDefaults({
submitHandler: function() {
alert("submitted!");
}
});
// validate signup form on keyup and submit
$("form").validate({
rules: {
pass: {
required: true,
minlength: 5
},
pass2: {
required: true,
minlength: 5,
equalTo: "#pass"
}
},
messages: {
pass: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long"
},
pass2: {
required: "Please provide a password",
minlength: "Your password must be at least 5 characters long",
equalTo: "Please enter the same password as above"
}
}
});
});