简体   繁体   中英

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:

    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:

<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. 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. In last if areEqual variable will be true because two empty strings are equal and cvPasswordValidation.IsValid = true; will be executed. In this case your text boxes will be valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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