![]() |
| | #1 |
| Registered User Join Date: Jan 2008
Posts: 17
| How to make a specific regex for C#? 1) Consist of at least one non alphanumeric character (not including white spaces) 2) At least 7 characters long So it would match something like... Hello!1 P@ssword but not: Hello1 1 Password111 |
| AsleeKaizoku is offline | |
| | #2 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| Alphanumeric characters are "[a-zA-Z0-9]", so non-alphanumeric would be "[^a-zA-Z0-9]". You don't mention any rules of the oterh characters, so I'll assume almost any can do. This is the pattern ".". The final expression could then be ".*[^a-zA-Z0-9].*". Note that * is greedy (look this up!) so you may need to use ".*?" instead of ".*", but I don't think it matters in this case. As for the length of at least 7 it gets a little trickier. Can't you just check the length in code?
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #3 |
| Registered User Join Date: Jan 2008
Posts: 17
| Ahhh! Bloddy hell, why didn't I think of that? That makes it so much easier ![]() Code: Regex regexPassword = new Regex(@".*[^a-zA-Z0-9].*");
if (!regexPassword.IsMatch(txtPass.Text.ToString().Trim()))
{
lblError.Text = " * Must have at least 1 non-alphanumeric character";
lblError.Visible = true;
}
else if (regexPassword.IsMatch(txtPass.Text.ToString().Trim()))
{
string temp = txtPass.Text.ToString().Trim();
if (temp.Length < 7)
{
lblError.Text = " * Password length minimum: 7.";
lblError.Visible = true;
}
}
|
| AsleeKaizoku is offline | |
| | #4 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,125
| A few notes: 1) your "else if" is unneccessary, just an "else" will do (you perform that check already in the "if") 2) No need to call ToString() on TxtPass.Text, it is already a string
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #5 | |
| Registered User Join Date: Jan 2008
Posts: 17
| Quote:
(2) I keep forgetting that :P | |
| AsleeKaizoku is offline | |
| | #6 |
| Registered User Join Date: May 2003
Posts: 1,199
| You also said you wanted whitespace characters to not count. I'd do @".*[^a-zA-Z\s\d].*" for your regex. Anything that's not a letter, whitespace, or number.
__________________ You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards. |
| Cat is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to make some specific strings | hash-mobtadi | C++ Programming | 2 | 07-23-2008 11:33 AM |
| how to make a windows application | crvenkapa | C++ Programming | 3 | 03-26-2007 09:59 AM |
| make all rule | duffy | C Programming | 9 | 09-11-2003 01:05 PM |
| Question about atheists | gcn_zelda | A Brief History of Cprogramming.com | 160 | 08-11-2003 11:50 AM |
| 'functions' in make? | mart_man00 | C Programming | 1 | 06-21-2003 02:16 PM |