Reputation: 169
This jquery function working fine for button submit but not working for link button. Why?
<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$("#form1").validate({
rules: {
<%= txtUserName.UniqueID %>: {minlength: 5, required: true},
<%= txtPassword.UniqueID %>: {minlength: 5, required: true},
<%= txtEmail.UniqueID %>: {required: true},
<%= txtURL.UniqueID %>: {required: true},
<%= chkbox.UniqueID%>: {required:true},
<%= textcredit.UniqueID %>:{required:true},
},
messages: {
<%= txtUserName.UniqueID %>: {
required: "Plaese enter your name",
minlength: "User name must be atleaet of 5 characters"
},
<%= txtPassword.UniqueID %>: {
required: "Plaese enter your password",
minlength: "Password must be atleaet of 5 characters"
},
<%= txtEmail.UniqueID %>:{ required: "Plaese enter your Email Id",},
<%= txtURL.UniqueID %>:{ required: "Plaese enter Website URL",},
<%= chkbox.UniqueID %>:{ required: "Plaese select this chek box",} ,
<%= chkbox.UniqueID %>:{
required: "Plaese select creditcard",
minlength: "User name must be atleaet of 5 characters"
},
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table width="50%" cellpadding="2" cellspacing="4" style="border: solid 1px navy; background-color: #d5d5d5;">
</table>
</form>
</body>
Upvotes: 1
Views: 665
Reputation: 50728
The submit button posts back to the server naturally; the linkbutton posts back to the server using a __doPostBack
trick that Microsoft developed. That's why @Yuriy's solution is a good one. Alternatively, you could just have a common method:
function validateForm(id) {
return $("#" + id).valid();
}
And have all your buttons call your function. There are a couple of other JQuery/HTML 5 specific ways to handle this.
Upvotes: 0
Reputation: 22468
Add at the page's bottom this script:
<script type="text/javascript">
var originalDoPostBack = __doPostBack;
__doPostBack = function (sender, args) {
if ($("#form1").valid() === true) {
originalDoPostBack(sender, args);
}
}
</script>
Or add OnClientClick
property to LinkButton: OnClientClick="if(!$('#form1').valid()) return false;"
Upvotes: 5