• Status: Solved
• Priority: Medium
• Security: Public
• Views: 526

# Password Verification must have at least 2 alpha & 2 Numeric charcaters

I am new to javascript and need to check if a password field has at least a minimum of 2 alpha and 2 numeric

Here is my script to check for at least one of each but how do I change it to check for at least 2

alert("Password has to have at least one number and one letter character.");
return false;
}
return true;

thanks
0
guestsl
• 4
• 3
1 Solution

Commented:
Hi

/^([a-z]{2,}[0-9]{2,}[a-z0-9]*)|([0-9]{2,}[a-z]{2,}[a-z0-9]*)\$/i

But the code only checks if the letters and numbers are right after the other. You might have better success with this:

if (!(myLetterMatches && myDigits && myLetterMatches.length>=2 && myDigits.length >=2)) {
alert("Password has to have at least two numbers and two letter characters.");
return false;
}
return true;

Regards,
Zyloch
0

Author Commented:
thanks that realy worked very well, one last question related to password verification.

Another requeirement is that the maximum number of repeated characters is 2

Can you help with this requirements

0

Commented:
I must say that's putting quite a restriction on passwords. It's also making this a bit harder. You can do this:

var myLetterMatches = target.value.match(/[A-Za-z]/g);
var myDigits = target.value.match(/\d/g);

if (!(myLetterMatches && myDigits && myLetterMatches.length>=2 && myDigits.length >=2)) {
alert("Password has to have at least two numbers and two letter characters.");
target.focus();
target.select();
return false;
}

var myDuplicates = target.value.match(/^.*([A-Za-z0-9]{1}).*(\1).*\$/g);

if (myDuplicates) {
target.focus();
target.select();
return false;
}

return true;

0

Author Commented:
I also think its a big restricition, but I only do what I am told, our security is bigger than fort Knox.

Thanks for you help it worked a treat.
0

Commented:
0

Author Commented:
I thought I had this code working ok, but It will hceck for Duplicates of characters but they are allowed to have the same character twice
For Example
pass78 is ok but passs78 is not
How do I change the Duplicate checking code to look for more than 2 character duplication

thanks

var myDuplicates = target.value.match(/^.*([A-Za-z0-9]{1}).*(\1).*\$/g);

if (myDuplicates) {
target.focus();
target.select();
return false;
}

return true;
0

Commented:
Sorry for the late reply, I was on vacation. I'm not too sure what the problem is. I have this test code

<script>
function doh() {

var myLetterMatches = target.value.match(/[A-Za-z]/g);
var myDigits = target.value.match(/\d/g);

/*if (!(myLetterMatches && myDigits && myLetterMatches.length>=2 && myDigits.length >=2)) {
alert("Password has to have at least two numbers and two letter characters.");
target.focus();
target.select();
return false;
}*/

var myDuplicates = target.value.match(/^.*([A-Za-z0-9]).*(\1).*\$/g);

if (myDuplicates) {
target.focus();
target.select();
return false;
}

return true;
}
</script>
<body>
<form name="theForm">
</form>

and I tried both pass78 and passs78 and both return the alert.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.