Issue with Regular Expression Validator for text box in ASP.NET

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x