按钮单击中的asp代码隐藏之前优先级Jquery验证

时间:2015-08-14 09:39:45

标签: javascript c# jquery asp.net

ASP.net中的新功能,我希望在我的asp程序进入Codebehind之前先设置验证,因为在单击ASP内容按钮后,字段不会验证:内容

继承我的短代码:

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="MainContent" CssClass="bodyForm">
<section id="content" role="main">
                            <div class="entry two_third">
                                    <script type="text/javascript">
                                        jQuery(document).ready(function () {
                                            jQuery('#contactform').validationEngine('init');


                                            jQuery('#contactform #contact_form_formsend').click(function () {
                                                alert("He Goes Here!");
                                                var form_builder_url = jQuery('#contact_form_url').val();

                                                jQuery('#Content2 .loading').animate({ opacity: 1 }, 250);

                                                if (jQuery('#Content2').validationEngine('validate')) {
                                                    jQuery.post(form_builder_url, {
                                                        contact_name: jQuery('.txtName').val(),
                                                        contact_email: jQuery('.txtEmail').val(),
                                                        contact_subject: jQuery('.txtSubject').val(),
                                                        contact_message: jQuery('.txtaMessage').val(),
                                                        formname: 'Content2',
                                                        formtype: 'contactf'
                                                    }, function () {
                                                        jQuery('#Content2 .loading').animate({ opacity: 0 }, 250);

                                                        document.getElementById('Content2').reset();

                                                        jQuery('#Content2').parent().find('.box').hide();
                                                        jQuery('#Content2').parent().find('.success_box').fadeIn('fast');
                                                        jQuery('html, body').animate({ scrollTop: jQuery('#Content2').offset().top - 100 }, 'slow');
                                                        jQuery('#Content2').parent().find('.success_box').delay(5000).fadeOut(1000);
                                                    });

                                                    return false;
                                                } else {
                                                    jQuery('#Content2 .loading').animate({ opacity: 0 }, 250);

                                                    return false;
                                                }
                                            });
                                        });

                                    </script>
                                    <form action="#" method="post" id="contactform">
                                    <asp:Label ID="lblSuccess" runat="server" Text=""></asp:Label>
                                    <br />
                                        <p>Required fields are marked <span class="color_3">*</span></p>
                                        <div class="form_info cmsms_input">
                                            <label for="contact_name">Name <span class="color_3">*</span></label>
                                            <div class="form_field_wrap">
                                                <input type="text" name="contact_name" runat="server"  id="txtName" value="" size="22" tabindex="3" class="txtName validate[required,minSize[3],maxSize[100],custom[onlyLetterSp]]"/>
                                            </div>
                                        </div>
                                        <div class="cl"></div>
                                        <div class="form_info cmsms_input">
                                            <label for="contact_email">Email <span class="color_3">*</span></label>
                                            <div class="form_field_wrap">
                                                <input type="text" name="contact_email" runat="server"  id="txtEmail" value="" size="22" tabindex="4" class="txtEmail validate[required,custom[email]]" />
                                            </div>
                                        </div>
                                        <div class="cl"></div>
                                        <div class="cl"></div>
                                        <div class="form_info cmsms_input">
                                            <label for="contact_subject">Subject <span class="color_3">*</span></label>
                                            <div class="form_field_wrap">
                                                <input type="text" name="contact_subject" runat="server" id="txtSubject" value="" size="22" tabindex="6" class="txtSubject validate[required,minSize[3],maxSize[100]]" />
                                            </div>
                                        </div>
                                        <div class="cl"></div>
                                        <div class="form_info cmsms_textarea">
                                            <label for="contact_message">Message <span class="color_3">*</span></label>
                                            <div class="form_field_wrap">
                                                <textarea name="contact_message" runat="server"  id="txtaMessage" cols="28" rows="6" tabindex="7" class="txtaMessage validate[required,minSize[3]]" ></textarea>
                                            </div>
                                        </div>
                                        <div class="cl"></div>
                                        <div class="cl"></div>
                                        <div>
                                        <asp:Button class="more_button fl" runat="server" tabindex="8" Text="Send a message" 
                                                   ID="contact_form_formsend" onclick="btnSendEmail_Click" /></div>
                                            <div class="loading">

                                        </div>
                                        </form>
                                </div>
                            </div>
                        </section>
</asp:Content>

在这里你可以看到,我的问题是它继续代码隐藏并提交表单甚至在验证我遇到错误的页面之前因为空字段,希望有金心的人帮助我解决这个问题。 。在此先感谢

1 个答案:

答案 0 :(得分:1)

您可以在回发前调用javascript函数。 OnClientClick用于在回发之前调用javascript函数。使用以下代码作为参考:

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" ToolTip="Save" OnClientClick="return CheckValidation()"  />

调用CheckValidation() javascript函数。如果函数返回true,它将回发。

<script type="text/javascript">
function CheckValidation()
{
// do validations here and return true if validate and return false if not
}
</script>