链接文本框控件的必填字段和正则表达式验证器

Issue with chaining Required Field and Regular Expression validators for Textbox controls(链接文本框控件的必填字段和正则表达式验证器的问题)
本文介绍了链接文本框控件的必填字段和正则表达式验证器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ASP.net 实现表单验证,并且尝试了所有建议的解决方案 此处 但最好的是 aspsnippets.com 到目前为止.

I'm trying to implement a form validation with ASP.net and I have tried every solution suggested here but the best one was on aspsnippets.com so far.

我的代码如下:

<asp:TextBox ID="tTitle" runat="server" onblur="WebForm_OnBlur()"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="tTitle"/>

<asp:TextBox ID="tEMail" runat="server" onblur="WebForm_OnBlur()"/>
<asp:RequiredFieldValidator runat="server" ControlToValidate="tEMail"/>
<asp:RegularExpressionValidator runat="server" ControlToValidate="tEMail"
ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*"/>

<asp:LinkButton ID="btnSubmit" runat="server" Text="Submit"/>

Javascript

<script type="text/javascript">
function WebForm_OnSubmit() {
    if (typeof (ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
    {
        for (var i in Page_Validators) {
            try {
                var control =
                document.getElementById(Page_Validators[i].controltovalidate);
                if (!Page_Validators[i].isvalid) {
                    control.className = "error";
                } else {
                    control.className = "";
                }
            } catch (e) { }
        } return false;
    } return true;
}
function WebForm_OnBlur() {
    for (var i in Page_Validators) {
        try {
            var control =
            document.getElementById(Page_Validators[i].controltovalidate);
            if (!Page_Validators[i].isvalid) {
                control.className = "error";
            } else {
                control.className = "";
            }
        } catch (e) { }
    } return false;
}
</script>

问题是电子邮件字段仅验证正则表达式.如果我更改验证器的顺序,它只会验证所需的表达式.

The problem is the e-mail field only validates for the regular expression. If I change the order of the validators, it only validates for required expression.

可能的问题是代码循环遍历所有验证器,但不会一次比较引用相同控件的验证器.这会导致只对控件应用最后一个验证器条件.

The possible problem is that the code loops through all the validators but does not compare the ones that reference the same control at once. This causes only the last validator condition to be applied on the control.

推荐答案

可能的问题是代码循环遍历所有验证器,但不会一次比较引用相同控件的验证器.这会导致只对控件应用最后一个验证器条件.

The possible problem is that the code loops through all the validators but does not compare the ones that reference the same control at once. This causes only the last validator condition to be applied on the control.

是的,这确实是问题所在.要修复它,您可以执行以下操作:

Yes, this is indeed the problem. To fix it, you can do the following:

WebForm_OnBlur 函数中,循环遍历与失去焦点的控件关联的验证器(而不是页面上的所有验证器),并仅清除 className 属性如果所有验证器都有效:

In the WebForm_OnBlur function, loop through the validators associated with the control that lost focus (rather than all the validators on the page), and clear the className property only if all the validators are valid:

function WebForm_OnBlur(control) {
    for (var i = 0; i < control.Validators.length; i++) {
        if (!control.Validators[i].isvalid) {
            control.className = "error";
            return;
        }
    }
    control.className = "";
}

TextBox控件的onblur属性中,将this作为参数传递给WebForm_OnBlur:

In the onblur attribute of the TextBox controls, pass this as the argument to WebForm_OnBlur:

<asp:TextBox ID="tTitle" runat="server" onblur="WebForm_OnBlur(this)"/>
<asp:TextBox ID="tEMail" runat="server" onblur="WebForm_OnBlur(this)"/>

WebForm_OnSubmit 函数中,为每个具有关联验证器的控件调用WebForm_OnBlur:

In the WebForm_OnSubmit function, call WebForm_OnBlur for each control that has associated validators:

function WebForm_OnSubmit() {
    if (typeof(ValidatorOnSubmit) === "function" && ValidatorOnSubmit() === false) {
        for (var i = 0; i < Page_Validators.length; i++) {
            var control = document.getElementById(Page_Validators[i].controltovalidate);
            if (Page_Validators[i] === control.Validators[0]) // minor optimization
                WebForm_OnBlur(control);
        }
        return false;
    }
    return true;
}

这篇关于链接文本框控件的必填字段和正则表达式验证器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)