6

I'm using a regex below to validate password to accept alphanumeric characters only. The regex works if I enter 2 characters one alpha and one number but if more than two characters my regex doesn't work. I want if possible the following results as shown in "Expected Behavior". Can anyone help me rewrite my regex?

JavaScript

 function checkPasswordComplexity(pwd) { var regularExpression = /^[a-zA-Z][0-9]$/; var valid = regularExpression.test(pwd); return valid; } 

Current Behavior

Password:Valid a1:true aa1:false aa11:false 

Expected Behavior

Password:Valid aa:false (should have at least 1 number) 1111111:false (should have at least 1 letter) aa1:true aa11:true a1a1a1a1111:true 
2
  • Use match() instead of test() Commented May 29, 2013 at 12:36
  • 5
    Don't limit characters allowed in password, it only makes them weaker. Commented May 29, 2013 at 12:37

6 Answers 6

17

You want to add "one or more", you're currently checking for a letter followed by a number.

Try:

/^[a-zA-Z0-9]+$/ 

+ means 'one or more'

I also joined the ranges.

Note: I don't understand why you'd want to limit the password to such a small range though, having a wide character range will make your passwords stronger.

Here is a fiddle demonstrating the correct behavior

If you just want to validate that the password has at least one letter and at least one number, you can check like this:

function checkPasswordComplexity(pwd) { var letter = /[a-zA-Z]/; var number = /[0-9]/; var valid = number.test(pwd) && letter.test(pwd); //match a letter _and_ a number return valid; } 
Sign up to request clarification or add additional context in comments.

4 Comments

From my understanding, the OP wants the string to have at least one letter AND at least one number.
@Benjamin Gruenbaum thanks for this! It solved my issue. Well with regards to the simplest password complexity it's the needs our our client to have an aphanumeric password only. In fact some local banks here still have alphanumeric password only. Even us still wondering why they haven't switch to complex password. Thanks once again for the help!
You're welcome. It is my experience, that when working in teams where not everyone is a regex wiz, it is better to avoid things like look-aheads and look-behinds unless you are sure people understand them. It is a nice and powerful concept, but it makes code that is unreadable for people who only know the basic regular expressions. Good luck!
If the aim of the solution is to validate inputs that contain BOTH letters and numbers then this does not work.
5
function checkPasswordComplexity(pwd) { var regularExpression = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/; var valid = regularExpression.test(pwd); return valid; } 

Comments

2

You can use this:

/^(?=.*\d)(?=.*[a-z])[a-z\d]{2,}$/i 

Comments

1

Try doing this:

var regularExpression = /^[a-zA-Z0-9]+$/; 

This means "one or more letter or number."

However, some users might also want to enter symbols (like &*#) in their passwords. If you just want to make sure there is at least one letter and number while still allowing symbols, try something like this:

var regularExpression = /^(?=.*[a-zA-Z])(?=.*[0-9]).+$/; 

The (?=.*[a-zA-Z]) is a positive lookahead. This means that it makes sure that there is a letter ahead of it, but it doesn't affect the regex.

1 Comment

The second one will never match, you want /^(?=.*[A-Z]) etc.
1
{ var pwd=document.getElementById('pwd').value; var reg = /^[a-zA-Z0-9]{8,}$/; var re=reg.test(pwd); alert(re); 

}

Comments

0

I think lookaround aren't supported by javascript, so you can use:

^([a-zA-Z]+\d+)|(\d+[a-zA-Z]+) 

But if they are supported:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.