Issue with Regular Expression Validator for text b

2020-05-07 08:19发布

问题:

Im having issues with the RegularExpressionValidator in my code!

I have 2 text boxes in my html page and in the code i have the sql command that inserts the parameters to the DB, the issue is that if i put a special character to the text box the sql stops because i need the validators, so i tried validating like this.

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>

The validator is defined as:

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

But when loading it ignores my validation expresion.

Please what is missing.

Thanks

回答1:

I checked everything and finally got it,

I had to add to my web.config the following:

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

And that solved my issues



回答2:

If you want to see a mark next to the field when validation fails, you should set the Text property of the validator:

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

Also, if you want to see the error message, you must have a ValidationSummary in your form.