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

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.

Thanks,
Hetal Gadhavi
Posted
Comments
Sampath Kumar Sathiya 26-Dec-12 2:40am    
Hi,

Try to find answer from this,

http://www.regexlib.com
Sergey Alexandrovich Kryukov 26-Dec-12 3:16am    
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.
—SA

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:
C#
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
    }
 
Share this answer
 

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



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