tags:

views:

878

answers:

6

Need help with a regex for alphanumeric password, with at least 1 number and character, and the length must be between 8-20 characters.

I have this but doesn't seem to be working (it doesn't have length requirements either):

^[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*$
A: 
^(?=.{8,20}$)(?=.*[0-9])(?=.*[a-zA-Z]).*

? :)

cwap
It seems like the .* at the end will match even special characters.
bmb
A: 

Wouldn't it be better to do this validation with some simple string functions instead of trying to shoehorn a difficult to validate regex into doing this?

Sanjay Sheth
Meh, sometimes Regex'es are easier for this kind of (Password?)-validation. Especially since you can use the Asp.Net regex-validators :)
cwap
Sanjay, input validation is one of the most frequent applications of RegEx, and using them gives you more flexibility if/when the validation requirements change.
Adam Robinson
Regex validation doesn't tell you what's wrong with failing input, other than "this didn't match the regex," so you can't tell the user specifically what he did wrong. Which, in a lot of circumstances, is OK.
Robert Rossney
+2  A: 

If you take a look at this MSDN link, it gives an example of a password validation RegEx expression, and (more specifically) how to use it in ASP.NET.

For what you're looking to accomplish, this should work:

    (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,20})$

This requires at least one digit, at least one alphabetic character, no special characters, and from 8-20 characters in length.

Adam Robinson
A: 

Something like this will be closer to your needs. (I didn't test it, though.)

Regex test = new Regex("^(?:(?<ch>[A-Za-z])|(?<num>[9-0])){8,20}$");
Match m = test.Match(input);
if (m.Success && m.Groups["ch"].Captures.Count > 1 && m.Groups["num"].Captures.Count > 1)
{
  // It's a good password.
}
John Fisher
A: 

Why not just use a handful of simple functions to check?

checkPasswordLength( String password);
checkPasswordNumber( String password);

Maybe a few more to check for occurrences of the same character repeatedly and consecutively.

S Pangborn
Input validation is something that regex is great for, and using a RegEx expression gives you more flexibility in the future.
Adam Robinson
They're not "great" for it when you have do non-matching lookaheads and the like. If you do use regex, there's no reason to shoehorn everything into a single regex. +1 for a sane answer.
John Kugelman
@Adam Robinson - who says checkPasswordLength() can't use a RegEx within the function? Nothing's stopping you from using their advantages in this solution.
ceejayoz
@ceeja, @John: The flexability and speed of RegEx comes largely from being able to check things (at least from a development perspective) in a single pass. Breaking it up into two statements either explicitly or via encapsulation in multiple functions, will lessen it. It also seems to be overengineered for something so simple. I fail to see how using a regex to validate input is "insane".
Adam Robinson
A: 
Raj kumar pandey