Asp.Net自定义验证控件不显示错误消息

时间:2011-08-25 10:06:47

标签: c# .net asp.net validation

以下代码用于验证表单提交按钮何时被点击,但我没有得到想要的结果。

它应该显示错误消息,但它们没有显示我做错了什么?

自定义验证程序验证密码,它应根据发生的错误更改错误消息:

  1. 密码为空白
  2. 密码无效
  3. 密码不匹配
  4. 一次只能显示一个错误,所以我在.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;
            }
    
        }
    

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

在分配给IsValid属性后,你应该使用return。完全没有指定密码时的Condider情况。如果在分配后未指定return,则将执行所有三个if语句。在上一个if areEqual变量中将true,因为两个空字符串相等,并且将执行cvPasswordValidation.IsValid = true;。在这种情况下,您的文本框将有效。