Javascript Regex Validation

Hello

I need some help with validating the password strength of my form.

I am actually looking for a regular expression that could force the users to have at least 2 of the following:

  1. Upper and lower case.
  2. Numbers
  3. Symbols.

Thanks in advance for any help

A regular expression won’t help you with this. Instead, you will want to set flags for chars/numbers/symbols to false, and loop through each character of the password, setting each flag as is appropriate. After the loop, check to see if all flags are set.

Hi

Someone suggested me this solution and I am sharing it with you all :slight_smile:


<script>
function validate(){

	var pw = 'dddDd##';

	if (  ( pw.match(/[a-z]/) && pw.match(/[A-Z]/) ? 1 : 0 ) +
	     ( pw.match(/\\d/) ? 1 : 0 ) +
	     ( pw.match(/[\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\-\\+\\=\\[{}\\\\\\}\\:\\;\\"\\'\\<\\,\\>\\.\\?\\/]/) ? 1 : 0 )
	     >= 2 ) 
	{
		    alert('true');
	} else {
		    alert('false');
	}
}

</script>
<input type="button" onclick="validate()" />


There’s a problem there though. The code is matching a sample password of ‘dddDd##’ which doesn’t contain any digits, but don’t you require at least two digits?

I maintain that looping through the password setting flags for what you require, and afterwards checking that all those flags have been set, is the easiest way to achieve what you require.

No sir

As I mentioned in my initial post, that atleast 2 of those condition should match.

In the string ‘dddDd##’, the 1st and 3rd condition matches which is exactly I am looking for.

Then congrats, it seems that you have something which uses multiple regular expressions to achieve your needs.