dirty sanchez
dirty sanchez

Reputation: 25

Javascript regular expression

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}$  

Upvotes: 2

Views: 63

Answers (1)

Anirudha
Anirudha

Reputation: 32787

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

Upvotes: 4

Related Questions