right to the point. Have this form:
<form action="/Home/AskQuestion" method="post" id="question">
<div class="name">
<label>Name</label><br>
<input type="text" class="name required">
</div>
<div class="email">
<label>Email</label><br>
<input type="text" class="email required">
</div>
<div class="subject clearleft"><label>Subject</label><br> <select name="GroupId" class="emailSubject required" id="SubmitAQuestionEmailSubject"><option value="0">Customer Service</option><option value="1">Parts Request</option><option value="2">Product Question</option><option value="3">GCRR</option></select></div>
<div class="question clearleft">
<label>Question</label><br>
<textarea cols="20" rows="2" class="required"></textarea>
</div>
<input type="submit" class="submit">
</form>
and this code:
$(document).ready(function(){ $("form").validate(); });
A google search for "jquery.validate example" gives me back all sorts of hits for a "working" sample just like this, which will prevent a form submission if any of the class="required" fields are blank. The example on the jquery validate page works for me.
My form submits anyway when I leave a "required" input blank. What the hell am I doing wrong?
Edit: Lost some info that was originally in this post somehow...
I am including both the jquery (1.6.1) and jquery validation (1.6 from the file header) script files. Jquery is included before jquery.validation. I've verified with firebug that the scripts are being found, and I'm not able to see any errors eithe开发者_如何学Cr in firebug or in the Firefox error console.
Edit2:
Interestingly, replacing the $("form").validate() with this code seems to trigger validation, but again, only the first input in the form (name) is validated...
$(".submit").click(function (evt) {
if ($("form#question").validate().form()) {
alert("valid");
}
});
Have you included the jQuery Validation plugin?
Update 1
Since you said that you have included the jQuery validation plugin. Then, the only thing I can suggest, to you, is to try this:
$(document).ready(function(){ $("#question").validate(); });
Update 2
You should add a minlength
and a maxlength
attribute to your inputs. Validation doesn't work like magic. It should get the properties to validate against from somewhere. So add a minlength="2"
for your Name
input element, for example.
Are you loading both jQuery and the validate plugin script?
In case you want to be lazy:
<script type="text/javascript" src="jquery-latest.min.js"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
UPDATE:
Each input
value needs a name=""
associated with it. The validation won't work otherwise.
The method validate()
is not built into JavaScript or jQuery. You need to define that method, or include the validation plugin Shef has mentioned.
EDIT:
Works for me: http://jsfiddle.net/pdt6h/
精彩评论