0

I have a textbox field. I want to put a validator for the textbox that if it contain any special character in name then need to throw exception. Below is the code I used but not working. Please let me know where I am doing wrong or provide any specific code

string input = txtName.Text;
Match match = Regex.Match(input, @"^[^/\\()~!#$%|<>:{}+?^&*""]");
if (!match.Success)
{
lblMessage.Text = txtName.Text + " detected! " + match.Value + " Prohibited characters are: ^, /, , (, ), ~, !, #, $, %, |, <, >, :, ,, {, }, +, ?, &, *, \", [, ] and cannot be saved.";
}
  • To code looks fine. It should work. Are you getting any error? Can you provide some sample data? – Aanchal Jun 9 '14 at 8:56
  • there is no error, code is built correctly... when I execute in my local or create console application it works fine.. but when i integrate with my code it does not work. textbox execute without validation – Brishal Jun 9 '14 at 9:34
  • Where are integrating? From web page or other application? – Aanchal Jun 9 '14 at 9:46
  • .net application – Brishal Jun 9 '14 at 12:41
  • Windows forms application or asp.net ? – Aanchal Jun 9 '14 at 12:45
0

You can use RegularExpressionValidator control for your validations .

You can check the following examples to implement this:

http://msdn.microsoft.com/en-us/library/ff650303.aspx http://www.codeproject.com/Tips/472728/RegularExpressionValidator-In-ASP-NET

Or you can validate your textbox input value with javascript validation methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.