点击“转到”按钮,即使在框中填写了无效值,也会显示所有正常消息。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="https://code.jquery.com/jquery-1.8.3.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js"></script>
<title></title>
<script type="text/javascript">
$(document).ready(function () {
jQuery.validator.addMethod('integer', function (value, element, param) {
return (value != 0) && (value == parseInt(value, 10));
}, 'Please enter a non zero integer value!');
$('#myform').validate({
rules: {
field1: {
required: true,
integer: true
},
field2: {
required: true,
integer: true
}
},
submitHandler: function (form) { // for demo
alert('valid form');
return false;
}
});
$('#go').click(function(){
if (!$("#myform").validate())
{
// Not Valid
alert('missing some data');
return false;
}
else
{
<!-- $("#myform").submit() -->
alert('all good');
return false;
}
});
});
</script>
</head>
<body>
<form id="myform">
<input type="text" name="field1" />
<br/>
<input type="text" name="field2" />
<br/>
<input id="go" value="GO" type="button" />
</form>
</body>
</html>
答案 0 :(得分:1)
不是&#34;如果&#34; .validate()
- 这是验证程序对象,因为它存在,所以始终为true
。
Use .valid()
for any boolean testing:
$('#go').click(function() {
if (! $("#myform").valid()) { ....