除非我输入文本和标签到下一个,否则我无法让我的表单验证在下面做出响应。单击不同的字段或点击提交什么都不做,我希望它在所有情况下都能响应。老实说,这是我第一次将 jquery 用于比启用或禁用类更复杂的事情,所以我可能在某处犯了一个基本错误。然而,我无法弄清楚它是什么。
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">
//form validation
$(document).ready(function() {
$("#form").validate({
rules: {
organization: {
required: true,
minlength: 2,
},
firstname: {
required: true,
minlength: 2,
},
lastname: {
required: true,
minlength: 2,
},
email: {
required: true,
email: true,
},
phone: {
required: true,
minlength: 10,
number: true,
},
},
messages: {
organization: "Please enter a valid organization name",
firstname: "Please enter your first name",
lastname: "Please enter your last name",
email: "Please enter a valid email address",
phone: "Please enter a valid phone number",
},
});
});
</script>
</head>
<body>
<div id="form_container">
<div id="form_title">REQUEST INFO</div>
<form id="form" name="form_container" action="#">
<!-- fields -->
<input id="organization" type="text" name="organization" minlength="2" />
<input id="firstname" type="text" name="firstname" minlength="2" />
<input id="lastname" type="text" name="lastname" minlength="2" />
<input id="email" type="text" name="email" />
<input id="phone" type="text" name="phone" />
<!-- submit button -->
<input type="button" id="button" name="submit" type="submit" value="submit" >
</form>
</div>
</body>
</html>