0

我有一个带有 id 的文本框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

已显示。如何避免范围验证器使用正则表达式验证器消息显示其错误消息?

4

1 回答 1

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";
            }
        }
于 2013-05-19T14:04:43.733 回答