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

发布于 2024-10-11 07:32:52 字数 1973 浏览 4 评论 0原文

我有一个简单的表单,它使用 jQuery 验证来通知用户输入错误等。当我向表单添加 ASP.NET CustomValidator 时,它会导致页面回发并跳过 jQuery 验证。我需要在客户端验证正确之前将表单提交到服务器。有人见过这个吗?

这是我的表单:

<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>

这是包含自定义验证器的 core:recaptcha 控件:

<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 事件正确处理验证码结果。

CustomValidator 的 ServerValidate 部分在回发期间工作正常,但我需要它停止干扰客户端验证。如果我从验证码控件中删除 CustomValidator,一切都会很好。

我是否需要在 CustomValidator 的 clientvalidation 函数中执行类似调用 jquery-validate 之类的操作才能使其正常工作?

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

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?

This is my form:

<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>

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装迷糊 2024-10-18 07:32:52

所以事实证明,这个问题的答案是在我的链接按钮上设置 CausesValidation="False" ,然后在链接按钮的 OnClick 处理程序中调用 Page.Validate() 。

这并不完全是我正在寻找的解决方案,因为每次我想使用 recaptcha(或任何与此相关的自定义验证器)时,我都必须记住执行这些操作,但这似乎目前有效。

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.

绝影如岚 2024-10-18 07:32:52

您可以设置 EnableClientScript将验证器的属性设置为 false

<asp:CustomValidator id="RecaptchaValidator" runat="server"
    ControlToValidate="DummyInput" OnServerValidate="ServerValidate"
    ValidateEmptyText="True" EnableClientScript="False" />

You can set the EnableClientScript property of your validator to false:

<asp:CustomValidator id="RecaptchaValidator" runat="server"
    ControlToValidate="DummyInput" OnServerValidate="ServerValidate"
    ValidateEmptyText="True" EnableClientScript="False" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文