Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Password validation - RegExp

Status
Not open for further replies.

r3g

Programmer
Apr 29, 2003
5
AU
Hi,

Simple RegExp request here, well I think it's simple. I'm looking for RegExp pattern that will match a string which contains at least 5 alphabetic characters [a-zA-Z] (not necessarily in sequence).

e.g:
wordword - would match
word121word - would match
w1w1w1w1w1 - would match
w1w1w1 - no match

Thanks
 
Im sure using some clever stuff like lookahead assertions etc you could just write and expression to test for this but another way to do it would be...
Code:
bool TestString(string s){
  RegEx r = new RegEx("[a-zA-Z]");
  MatchCollection mc = r.Matches(s);
  if(mc.Count >= 5) return true;
  else return false;
}
Rob

i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...
 
Same as cbb just added the length check in the regex in case you also want to use it in a rev in the aspx
[a-zA-Z]{5,20}
just states must be at least 5 and no more than 20.
Marty
 
Marty: Wouldn't the above only match instances of between 5 and 20 (inclusive) consecutive alpha characters so would fail to match as required? Thats why I went for testing MatchCollection.Count instead as the basic RegEx [a-zA-Z] matches one instance of an alpha character so if there are 5 or more matches in the collection you know there were at least 5 alpha characters.

I'm sure you can write an expression which handles the whole job and i'm pretty certain that forward (look-ahead) assertions are the trick to it. If this for a RegularExpressionValidator then the above code in a CustomValidator would do the trick in the meantime.

One downside of this approash though is there is no way to disallow other characters from the string without running a seperate expression to validate allowed characters. just depends on your requirements and how much you want to try for the truly eleganty solution ;-)

I've got 5 mins so I'll have a go at the expression anyway.

Rob

i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...
 
Lol - its actually much simpler than all that over complicated stuff I rambled about before. Just check for an alpha char followed by any number of optional numeric chars a minimum of 5 times like this
Code:
([a-zA-Z]\d?){5,}

Simple :)

I tested it against all of the conditions you provided above and a few more and it seems ok. this is easily extend to allow other characters as well but wouldn't allow invalid chars.

Rob

i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...
 
Rob,
Thank you very much. My bad, I did not read it close enough and worse I did not test it.
Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top