ASP.NET CustomValidator控件与jQuery验证干扰表单提交

问题描述:

我有一个使用jQuery验证当我添加一个ASP.NET的CustomValidator的形式通知输入错误等的用户一个简单的形式,它会导致页面回发并跳过jQuery验证。我需要直到客户端验证是正确的形式不被提交到服务器。有没有人见过这个?

I have a simple form that uses jQuery validation to notify the user of input errors, etc. When I add an ASP.NET CustomValidator to the form, it causes the page to postback and skip the jQuery validation. I need the form to not be submitted to the server until the client-validation is correct. Has anyone seen this before?

这是我的方式:

<form id="form1" runat="server">  
    <core:standardscriptmanager runat="server" />
    <div>
        <asp:textbox id="Email" name="Email" runat="server" cssclass="_RegisterFormValidate FormField Email {required:true, email:true, messages:{required:'You must enter an email address.', email:'Please enter a valid email address.'}}" maxlength="200" columns="75" width="98%" tooltip="Your email address"></asp:textbox>
        <br /><br />
        <core:recaptcha runat="server" />
        <br />
        <asp:linkbutton id="CreateAccount" runat="server" onclick="CreateAccount_Click" text="create"></asp:linkbutton>
    </div>
</form>

这是核心:包含自定义验证验证码控制:

And this is the core:recaptcha control that contains the custom validator:

<script type="text/javascript"
 src="http://www.google.com/recaptcha/api/challenge?k=XXXKEY">
</script>
<noscript>
 <iframe src="http://www.google.com/recaptcha/api/noscript?k=XXXKEY"
     height="300" width="500" frameborder="0"></iframe><br>
 <textarea name="recaptcha_challenge_field" rows="3" cols="40">
 </textarea>
 <input type="hidden" name="recaptcha_response_field"
     value="manual_challenge">
</noscript>

<asp:customvalidator id="RecaptchaValidator" runat="server" controltovalidate="DummyInput" onservervalidate="ServerValidate" validateemptytext="true" />
<asp:textbox id="DummyInput" runat="server" cssclass="Hidden"></asp:textbox>

注:DummyInput是唯一有使的CustomValidator开心,我的ServerValidate事件的结果验证码正确处理。

Note: The DummyInput is only there to make the CustomValidator happy, my ServerValidate event correctly deals with the captcha results.

在的CustomValidator的一部分的ServerValidate回发过程中,工作正常,但我需要它停止与客户端验证干扰。如果我从验证码控制移除的CustomValidator,一切都很好发挥。

The ServerValidate portion of the CustomValidator is working fine during the postback, but I need it to stop interfering with the client-side validation. If I remove the CustomValidator from the recaptcha control, everything plays nice.

我需要做的的CustomValidator的clientvalidationfunction类似电话的jQuery,验证,以便正确地使这项工作?

Do I need to do something like call jquery-validate in the CustomValidator's clientvalidationfunction in order to make this work correctly?

任何想法或建议,将受到欢迎。

Any thoughts or suggestions would be welcome.

因此​​,原来的答案,这个问题是设置的CausesValidation =我的LinkBut​​ton的假,然后在LinkBut​​ton的onclick处理程序调用Page.Validate()

So it turns out the answer to this issue was to set CausesValidation="False" on my linkbutton and then call Page.Validate() in the linkbutton's OnClick handler.

这不正是我一直在寻找,因为我必须记住我想要使用验证码(或与此有关的任何自定义验证),每次做这些事情,但是这似乎现在的工作方案。

It's not exactly the solution I was looking for since I'll have to remember to do those things every time I want to use recaptcha (or any custom validator for that matter) but this seems to work for now.