0

输入'12',点击按钮,a为假,b为真...注意EnableClientScript设置为假。


<asp:TextBox ID="TextBox1" runat="server"
     Width="20px" MaxLength="3"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
     runat="server" EnableClientScript="false" ControlToValidate="TextBox1"
     ErrorMessage="RegularExpressionValidator"
     ValidationExpression="([1-9]|[1-9][0-9]|[1-9][0-9][0-9])"
     ForeColor="Red"><sup>invalid</sup></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

protected void Button1_Click(object sender, EventArgs e)
{
  Page.Validate();
  bool a = RegularExpressionValidator1.IsValid;
  bool b = System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text,
             RegularExpressionValidator1.ValidationExpression);
}
4

1 回答 1

0

替换ValidationExpression为:

ValidationExpression="^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$"
于 2012-05-16T07:54:50.067 回答