Hi, I'm using jQuery+asp.net webforms, also I'm using jQuery Validation plugin and Fancybox plugin. My code works fine when I use the plugins separately. My webform is for user registration.
I use validation plugin to validate my form before it's submitted to the server, the form is validated and submitted when the user clicks an asp.net image button (which renders as an input type="image" html tag, ID="btnSave"). It already works as expected.
The fancybox plugin is used to display a pdf document which is generated dinamically on another webform. It works when the user clicks an anchor ("a" element, ID="btnShowFancybox").
I want to validate the form using the validation plugin when the user click the button btnShowFancybox, and if validation succes, show fancybox to display the pdf document. If validation fails, I want to show a message (alert) to the user. I've not been able to achieve this functionality.
This is what I've already coded:
HEAD:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#aspnetForm").validate({
//commented for simplicity
});
});
</script>
</head>
FORM:
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/content/img/save.png"
OnClick="btnSave_Click"
OnClientClick="return jQuery('#aspnetForm').validate().form();" />
<a id="btnShowFancybox" class="iframe" href="generatePDF.aspx">
<img src="content/img/showPDF.png" />
</a>
I need to implement validation of the form, after user clicks btnShowFancybox, if validation fails do not show the fancybox. I've tried this:
//changed href attribute of btnShowFancybox to "#", and inserted this code
//after: jQuery(document).ready(function() {
//and before: jQuery("#aspnetForm").validate({
//what I'm trying to do here is to set the proper href only if form is valid,
//then to call my fancybox to show pdf to the user, because the call to .fancybox didn't work I tried trigger the click event directly. and.. didn't work.
jQuery("#btnShowFancybox").click(function (e) {
var validForm = jQuery('#aspnetForm').validate().form();
alert(validForm);
if(validForm) {
jQuery("#btnShowFancybox").attr("href", "generatePDF.aspx");
jQuery("#btnShowFancybox").fancybox({
'frameWidth' : 1000,
'frameHeight': 700,
'overlayShow': true,
'hideOnContentClick': false
});
jQuery("#btnShowFancybox").fancybox().trigger('click');
}
else {
alert("form is not valid");
jQuery("#btnShowFancybox").attr("href", "#");
}
});
I also tried to create a JS function and set onclick attribute of btnShowFancybox to call this function. the function validates the form and if it succeded tried jQuery("#btnShowFancybox").fancybox(..., but it didn't work neither. Just to clarify, btnSave should not show fancybox, only btnShowFancybox should.
Help required please...