Solved

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

Posted on 2004-08-11
7
498 Views
Last Modified: 2012-05-05
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

 if(!theForm.Password.value.match(/^([a-z]+[0-9]+[a-z0-9]*)|([0-9]+[a-z]+[a-z0-9]*)$/i)){
     alert("Password has to have at least one number and one letter character.");
     theForm.Password.focus();
     return false;
  }
return true;

thanks
0
Comment
Question by:guestsl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11779506
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:


var myLetterMatches = theForm.Password.value.match(/[A-Za-z]/g);
var myDigits = theForm.Password.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.");
   theForm.Password.focus();
   return false;
}
return true;

Regards,
Zyloch
0
 

Author Comment

by:guestsl
ID: 11780402
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

Once again thanks for your help in advance
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 125 total points
ID: 11780690
I must say that's putting quite a restriction on passwords. It's also making this a bit harder. You can do this:


var target = theForm.Password;

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) {
   alert("Password may contain no duplicate characters.");
   target.focus();
   target.select();
   return false;
}
   
return true;

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:guestsl
ID: 11780702
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 11780723
No problem, glad to help
0
 

Author Comment

by:guestsl
ID: 11790115
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) {
   alert("Password may contain no duplicate characters.");
   target.focus();
   target.select();
   return false;
}
   
return true;
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11862093
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 target = theForm.Password;

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) {
   alert("Password may contain no duplicate characters.");
   target.focus();
   target.select();
   return false;
}

return true;
}
</script>
<body>
<form name="theForm">
<input type="text" name="Password" onchange="doh();">
</form>


and I tried both pass78 and passs78 and both return the alert.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question