阻止Range Validator在asp.net中显示带有正则表达式验证器的错误消息

时间:2013-05-19 03:20:43

标签: c# asp.net .net validation

我有一个标识为txtNumOfBooks的文本框,我正在验证以下3个验证器:

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtNumOfBooks" Display="Dynamic" ErrorMessage="Please enter the number of books" SetFocusOnError="True" CssClass="style4" ValidationGroup="CreateVac">
</asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtNumOfBooks" Display="Dynamic" ErrorMessage="Please enter the valid number" ValidationExpression="[0-9]\d*[0-9]" CssClass="style4" ValidationGroup="CreateVac">
</asp:RegularExpressionValidator>

<asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="txtNumOfBooks"
Display="Dynamic" ErrorMessage="Number of books in a book shelf should be in range 1-100"
MaximumValue="100" MinimumValue="1" Type="Integer" CssClass="style4" ValidationGroup="CreateVac">
</asp:RangeValidator>


但是,每当我在文本框中输入两个消息中的字符时:

Please enter the valid number Number of books in a book shelf should be in range 1-100
显示

。如何避免范围验证器显示带有正则表达式验证器消息的错误消息?

1 个答案:

答案 0 :(得分:0)

最后,找到了答案。需要添加一个javascript函数来删除不必要的范围验证器消息。该函数是:

  function clearRangeMessagePosition() {
            if (document.getElementById('<%=RegularExpressionValidator1.ClientID %>').getAttribute('isValid') != true.toString()) {
                document.getElementById('<%=RangeValidator1.ClientID %>').innerText = "";
            }
            else {
                document.getElementById('<%=RangeValidator1.ClientID %>').innerText = "Please enter number of books in range 1-100";
            }
        }