如何检查RegularExpressionValidator的状态

时间:2013-05-05 13:22:01

标签: asp.net vb.net

我有一个程序,有几个RegularExpressionValidator,它们会过滤掉用户的非数字输入。

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" class="errorMess" ErrorMessage="Letters are not Allowed!!" ControlToValidate="CelNo" ValidationExpression="\d+" runat="server" />

但是我发现RegularExpressionValidators只打印出错误消息,但不会阻止用户输入错误的数据。

因此我想知道如何在后端代码中验证RegularExpressionValidator,以便在我将值传递给查询之前设置限制。

例如,如果消息可见,则执行将停止。当然,还有一些其他方法可以过滤掉非数字数据。但我只想通过RegularExpressionValidator讨论它。

我试过这样的话。但无法理解为什么它不起作用:

    If RegularExpressionValidator1.IsValid Then

        MsgBox("Ya")

    Else
        MsgBox("Niht")
    End If

这是前端代码:

<asp:TextBox ID="CelNo" runat="server"></asp:TextBox>
   <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ErrorMessage="Letters are not Allowed!!" ControlToValidate="CelNo" ValidationExpression="\d+"
        runat="server" />
<asp:Button ID="Button5" runat="server" Text="Button" />

1 个答案:

答案 0 :(得分:2)

您可以使用Page.IsValid ...赞

进行检查
if(Page.IsValid)
{
   //your Stff
}
else
{
   //show your notification
}

或者你可以这样做....

if(YourRegularExpressionId.IsValid==true)
{
   //your Stff
}
else
{
   //show your notification
}
相关问题