Reputation: 125
Using ASP.NET, why is it that the bassistance.de jQuery validation plugin prevents form submission when using input type="submit" elements, and not html button elements?
The validation fires when using an html button (type="submit") tag, but the form is still submitted.
Is there a way to make the jQuery validation plugin work with html button elements?
A quick example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
$(document).ready(function () {
$("#form1").validate({
rules: {
txtFirstName: {
required: true
}
},
messages: {
txtFirstName: {
required: "* First Name required<br/>"
}
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<input id="txtFirstName" name="txtFirstName" type="text" />
<input id="btnSubmit1" type="submit" runat="server" value="Submit" /> <!-- WORKS! -->
<button it="btnSubmit2" type="submit" runat="server">Submit</button> <!-- DOESN'T WORK -->
</form>
</body>
</html>
Upvotes: 3
Views: 5225
Reputation: 268344
It appears to be designed that way:
// when a submitHandler is used, capture the submitting button
if (validator.settings.submitHandler) {
inputsAndButtons.filter(":submit").click(function () {
validator.submitButton = this;
});
}
Unfortunately that seems to be baked in, with no other option. The documentation on :select
seems to shed some more light on this:
The
:submit
selector typically applies to button or input elements. Note that some browsers treat element astype="default"
implicitly while others (such as Internet Explorer) do not.
One solution would be to bind your button
to a function that invokes the validation. In the example below we see this being done with an anchor:
$("#myform").validate();
$("a.check").click(function() {
$("#myform").valid();
});
Upvotes: 6