Validating a Password with Javascript with Regular Expressios

I'm getting some negative results when running a validation routine in JavaScript.
It seems to be failing on the part that checks for mixed case. Upper and lower letters.
I am typing what seems to be a valid entry of "aaa4aaaaZ" for the password but I'm getting back the message:
"You must use upper AND lower case letters. (At least one of each.) "
so obviously I'm doing something wrong with that call.

Can someone offer some advice on how to fix it?
I need to separate the regular expressions into multiple statements because I want to give the user
more detail as to where the error comes from.
So I know it's possible to wrap all of this in 1 statement but that will not favorable in this case.


validatePassword.txt
Garbonzo_HorowitzAsked:
Who is Participating?
 
nap0leonConnect With a Mentor Commented:
Instead of testing explicitly for "mixed case", test for upper separately from lower.

			// run reg ex for 1 uppercase letter and set result to var
			if(/[A-Z]/.test(temp)){
			  var fieldContainsAnUpperCaseLetter = true;
			}

			// run reg ex for 1 lowercase letter and set result to var
			if(/[A-Z]/.test(temp)){
			  var fieldContainsALowerCaseLetter = true;
			}

Open in new window

0
 
_agx_Commented:
> if(/^(?=.*[A-Za-z])(?=.*[0-9])(?!.*[^A-Za-z0-9])(?!.*\s).{8,15}$/.test(temp)){

I can't claim to follow what's going on there. But if it were me, I'd use two simpler tests instead:

1) Does it contain lower case?  /[a-z]/
2) Does it contain upper case? /[A-Z]/

If both are true, then you know it's mixed case
0
 
_agx_Commented:
Oops too late !

> /[A-Z]/.test(temp)

Small suggestion. IIRC test() returns true/false. So I think you can eliminate the if(...) statement and just capture the result.

ie      var result = /[A-Z]/.test(temp);
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Terry WoodsIT GuruCommented:
I don't have time to code it and test it, but what might be really neat for this scenario is an associative array of regular expressions and associate user feedback messages, something like:

var myArray = new Object();
myArray["^(?=.*[A-Z])"] = "Your password must contain at least one capital letter";
myArray["^.{8,15}$"] = "Your password must be between 8 and 15 characters long";

Then apply them one by one, and if they all pass then continue...
0
 
käµfm³d 👽Commented:
__No points__

It fails on mixed case because you are only checking one character:

temp = document.getElementById(field).value.charAt(i);

Open in new window


Since you specified that the pass should be between 8 and 15 characters, the value does not match the pattern.
0
 
_agx_Commented:
> is an associative array of regular expressions and associate user feedback messages

+100. That's a great idea!
0
 
Garbonzo_HorowitzAuthor Commented:
Splitting it into upper and lower case worked. Somtimes simple is much better.

Thanks for all your help.
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.

All Courses

From novice to tech pro — start learning today.