ASP.NET WebForms - 同时执行 JavaScript 验证和验证控制 [英] ASP.NET WebForms - Execute JavaScript validation and Validation Control at the same time

查看:27
本文介绍了ASP.NET WebForms - 同时执行 JavaScript 验证和验证控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在开发的应用程序中,我使用了 ASP.NET Web Forms 提供的验证控件,并且我使用了 JavaScript 的表单的某些部分.

In the application I'm developing, I using both the Validation controls provided by ASP.NET Web Forms and for certain parts of the form I'm using JavaScript.

验证的两个方面都正常工作,但是,当我单击提交"按钮时,首先进行 ASP.NET 验证,然后是我在 JavaScript 中进行的自定义验证.结果,当表单不完整并提交时,只显示 ASP.NET 验证错误,而当纠正这些错误并提交表单时,则显示我的验证错误.

Both aspects of the validation works as it should, however, when I click on the Submit button the ASP.NET validation comes first, then follows my custom validation made in JavaScript. As a result, when the form is incomplete and submitted, only the ASP.NET validation errors show, and when those errors are corrected and the form is submitted, then my validation errors are shown.

我想要的是,当用户单击提交"按钮时,ASP.NET 验证和我的 JavaScript 验证同时发生,以便用户可以同时看到所有错误.

What I want is that when a user clicks on the Submit button, both the ASP.NET validation and my JavaScript validation occur simultaneously so that users can see all the errors at once.

<form id="StudentLSFApplication" runat="server" onsubmit="return validateForm()">
    <!--other html code-->
    <asp:Button ID="SaveLSFApplication" runat="server" Text="Save Application" OnClick="saveApplication"
            ValidationGroup="AllValidation" />
</form>

<script>
    function validateForm() {
        //my validation
    }
</script>

正如您从我的示例代码中看到的,我在 onsubmit 事件和 Button 中调用了 JavaScript 函数 validateForm()控件是 AllValidation 验证组的一部分.目前,在表单提交时 AllValidation 首先发生,如果没问题,然后 validateForm() 发生.

As you can see from my sample code, I have the JavaScript function validateForm() being called in an onsubmit event, and Button control is part of the AllValidation validation group. At the moment, on form submit the AllValidation occurs first, and when that is okay, then validateForm() occurs.

推荐答案

我认为您正在寻找的是 CustomValidator.你可以这样使用它:

I think what you're looking for is the CustomValidator. You can use it like this:

    <script type="text/javascript">
        function validateFormElement(sender, args) {
            if (args.Value == "waarde") {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        }
    </script>

    <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="myValidation"></asp:TextBox>

    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Input error" ValidationGroup="myValidation" ClientValidationFunction="validateFormElement" ControlToValidate="TextBox1"></asp:CustomValidator>

    <asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="myValidation" />

而且您仍然可以使用所有其他验证器.无需将您想要的每项验证都塞进那个 javascript 函数中.

And you can still use all the other validators. No need to cram every validation you want into that one javascript function.

这篇关于ASP.NET WebForms - 同时执行 JavaScript 验证和验证控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆