ASP.NET中的文本框的正则表达式验证器问题

时间:2016-04-07 02:30:15

标签: c# sql asp.net c validation

我的代码中遇到了RegularExpressionValidator问题!

我的html页面中有2个文本框,在代码中我有sql命令将参数插入到数据库中,问题是如果我在文本框中放入一个特殊字符,则sql会停止,因为我需要验证器,所以我尝试验证这样。

Insert the files number.
<asp:TextBox ID="TextBox1" runat="server" MaxLength="10" ></asp:TextBox>

Whats the users Name <br />
<asp:TextBox ID="TextBox2" runat="server" MaxLength="10"></asp:TextBox>

验证器定义为:

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
    ErrorMessage="RegularExpressionValidator" ValidationExpression="^\d+$" 
    ControlToValidate="TextBox1"></asp:RegularExpressionValidator>

但加载它时会忽略我的验证表达。

请丢失什么。

由于

2 个答案:

答案 0 :(得分:1)

我检查了一切,最后得到了它,

我必须在web.config添加以下内容:

<configuration>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
</configuration>

这解决了我的问题

答案 1 :(得分:0)

如果要在验证失败时在字段旁边看到标记,则应设置验证程序的Text属性:

<asp:RegularExpressionValidator ID="rev1" runat="server" ControlToValidate="TextBox1" Text="*" />

此外,如果您想查看错误消息,则表单中必须有ValidationSummary