RegularExpressionValidator 错误消息不可见

RegularExpressionValidator Error Message not visible

我有以下 RegularExpressionValidator 只允许 4 位数字。

<asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtPinCode" Display="Dynamic" ErrorMessage="Please make sure to select a PIN with four numeric digits" ValidationExpression="^\d{4,4}$" CssClass="caption">4 Digits Pincode</asp:RegularExpressionValidator>

它会验证但不会显示错误消息:

Please make sure to select a PIN with four numeric digits

而是继续显示文本 4 Digits Pincode。我希望此文本被错误消息替换。怎么了?

添加 Display="None" 以便在您验证之前不会显示错误消息。

  <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtPinCode" Display="None" ErrorMessage="Please make sure to select a PIN with four numeric digits" ValidationExpression="^\d{4,4}$" CssClass="caption"></asp:RegularExpressionValidator>

您的“4 位 Pincode”文本进入文本 属性。这是 defined 作为

the text displayed in the validation control when validation fails

另一方面,ErrorMessage 属性 是

the text for the error message displayed in a ValidationSummary control when validation fails

因此它们都被视为 错误 消息,而不是 "one or the other"。

您也许可以使用“4 位数字”文本作为输入的占位符。或者只是始终在验证器旁边显示它。