Click here to Skip to main content
15,042,088 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:

I am working in C# Application, I want to do validation of textbox that It should not allow me more than one back slash and more than one hyphen. So can anyone tell me how to do using Regular expression validation?

I'll be grateful for your help.

Hetal Gadhavi
Sampath Kumar Sathiya 26-Dec-12 2:40am

Try to find answer from this,
What did you try so far? Or do you want someone to write the Regular Expression for you. Why? This is not how it works. Write one, explain what is the problem if you fail to achieve what you want.

1 solution

That isn't easy in a single regex - that isnt what they were created for, and it is a bit of a pain to implement, as you have to allow for all possible permutations yourself.

If you are doing this in C# as you say, then I would do it with two simple regexes:
Regex hyphens = new Regex("-");
Regex backslashes = new Regex(@"\\");
string inp = @"123-22-2\3";
if (hyphens.Matches(inp).Count > 1 || backslashes.Matches(inp).Count > 1)
    // Failed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900