简体   繁体   English

ASP.NET自定义验证控件未显示错误消息

[英]Asp.Net custom validation control not showing error messages

The following code is used to validate when form submit button is hit, but I am not getting the wanted result back. 以下代码用于验证何时按下表单提交按钮,但我没有得到想要的结果。

It should display the error messages but they are not being displayed what am I doing wrong? 它应该显示错误消息,但没有显示,我在做什么错?

The custom validator validates passwords, it should change the error message based on the which errors occur: 定制验证器验证密码,它应该根据发生的错误来更改错误消息:

  1. Password is blank 密码为空
  2. Password is invalid 密码无效
  3. Passwords do not match 密码不匹配

Only one error should show at a time so I wrote this custom validtor in .ascx.cs: 一次只能显示一个错误,因此我在.ascx.cs中编写了此自定义验证器:

    protected void cvPasswordValidation_ServerValidate(object source, ServerValidateEventArgs args)
    {

        bool IsPasswordBlank = false;
        bool IsPasswordValid = false;

        cvPasswordValidation.IsValid = false;

        // If password is blank then IsValid is false, show blank password errors.
        if (String.IsNullOrEmpty(args.Value))
        {

            //args.IsValid = false;
            IsPasswordBlank = true;
            //Show blank password error depending on which box is empty.
            if (String.IsNullOrEmpty(txtPassword.Text))
            {
                cvPasswordValidation.ErrorMessage = "You must enter password";
            }
            else
            {
                cvPasswordValidation.ErrorMessage = "You must confirm password";
            }
            cvPasswordValidation.IsValid = false;

        }
        else
        {
            cvPasswordValidation.IsValid = true;
        }

        // If password is not valid format then IsValid is false, show invalid format errors.
        if (!Regex.IsMatch(args.Value, RegexHelper.ValidPassword)) 
        {

            IsPasswordValid = true;
            //Show invalid format errors, if password is not blank
            if (IsPasswordBlank == false)
            {
                cvPasswordValidation.ErrorMessage = "<b>Current password</b> not in correct format. Your password must contain 6 - 12 characters with a combination of numbers and letters.";
            }
            cvPasswordValidation.IsValid = false;
        } 
        else 
        {
            cvPasswordValidation.IsValid = true; 
        } 

        // If passwords do not match then IsValid is false, show do not match errors.
        bool areEqual = String.Equals(txtPassword.Text, txtConfirmPassword.Text, StringComparison.Ordinal);

        if (areEqual == false)
        {

            //Show do not match errors is password is not blank and is not invalid format
            if (IsPasswordBlank == false && IsPasswordValid == false)
            {
                cvPasswordValidation.ErrorMessage = "Your passwords do not match.";
            }
            cvPasswordValidation.IsValid = false;
        }
        else
        {
            cvPasswordValidation.IsValid = true;
        }

    }

and the .ascx: 和.ascx:

<asp:TextBox runat="server" ID="txtPassword" MaxLength="12" autocomplete="off" TextMode="Password" ValidationGroup="vgOnlineDetails"></asp:TextBox>

<asp:CustomValidator ID="cvPasswordValidation"
                 runat="server"
                 display="Dynamic"
                 OnServerValidate="cvPasswordValidation_ServerValidate"
                 ControlToValidate="txtPassword"
                 ValidationGroup="vgOnlineDetails">
</asp:CustomValidator>

您需要设置ValidateEmptyText = true来验证空字段。

You shoul use return after assignment to IsValid property. 分配给IsValid属性后,应使用return Condider situation when you do not specify password at all. 根本不指定密码的情况。 If you do not specify return after assignment all three if statements will be executed. 如果您未指定赋值后的return ,则将执行所有三个if语句。 In last if areEqual variable will be true because two empty strings are equal and cvPasswordValidation.IsValid = true; 在最后if areEqual变量将是true ,因为两个空字符串相等和cvPasswordValidation.IsValid = true; will be executed. 将被执行。 In this case your text boxes will be valid. 在这种情况下,您的文本框将是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM