2

I am making form and there is only one more thing which I cant figure it out :(

I need regular expression for password which must be at least 7 characters long. There can be small and big letters and must contain at least one number.

I tried

[0-9]+[a-zA-Z]){7}$  
1
  • This looks pretty close to what you seek. Commented Jun 1, 2013 at 18:59

1 Answer 1

4

You can use lookahead:

^(?=.*\d)[a-zA-Z\d]{7,}$

(?=.*\d) is a lookahead which checks for a digit in the string. Basically, .* matches the whole string and then backtracks 1 by 1 to match a digit. If it matches a digit, the regex engine comes back to its position before match. So, it just checks for a pattern.

{7,} is a quantifier which matches previous pattern 7 to many times

^ is the beginning of a string

Sign up to request clarification or add additional context in comments.

2 Comments

I like this solution... simple and elegant.
@Anirudh Thank you very much, this looks so simple :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.