Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Must contain at least 6 alpha characters and at least 2 numbers. User ID and Passwords must not contain invalid characters and have a maximum length of 12 characters.

I need to get the regular expression from my database and verify. I dont want to check it in my c# code. I apologize for confusing.
Posted
Updated 20-Jan-16 22:43pm
v3
Comments
BillWoodruff 21-Jan-16 1:37am    
What have you tried so far ?
Navya Sri 21-Jan-16 2:21am    
^(?=.*[A-Z0-9a-z]{6,})(?=.*[0-9A-Z0-9a-z]{2,})[A-Za-z\d]{12,}$ I have tried this but here if i give a singl char and remaining numbers it is matching
BillWoodruff 21-Jan-16 3:10am    
"I cant check it in my c# code." why not ?

1 solution

It your numbers are freely mixed with letters, Regular Expression is quite unsuitable. Not only it's extremely hard to write the regular expression for such a mix, the idea of "any letter" is nearly impossible to express, if you want to allow all Unicode letters. With C#, it's easy to do. If you want, say, English characters only, it's also easier to do with C#.

Here is how: Fist, check up maximum length. Then traverse the string and count characters in 3 classes: letters, digits and the other. Before the cycle, initialize two counter variables to zero: one for letters and one for digits. In a loop, if non-letter and non-digit appears, return false (failure) immediately, otherwise increment the counter of letters of digits.

You can use the functions
Char.IsLetter Method (Char) (System)[^],
Char.IsDigit Method (Char) (System)[^],
or, even better, Char.IsLetterOrDigit Method (Char) (System)[^].

If you want to allow English characters only, just checkup that the character is between "A" and "z"; for other languages, the similar check can be not as simple.

That's all.

Just one note: the suggested criterion is really bad for a password. You can read recommendations on good password constraints in many places.

—SA
 
Share this answer
 
v2
Comments
Navya Sri 21-Jan-16 1:36am    
I need the exact regular expression
Sergey Alexandrovich Kryukov 21-Jan-16 1:38am    
No, you don't. This is the bad idea. But if you need it, create it by yourself. I promise that it will be huge and unmanageable. I can even estimate its length. :-)
I gave you exact and simple solution, just use it.
—SA

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