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.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
			  var fieldContainsAnUpperCaseLetter = true;

			// run reg ex for 1 lowercase letter and set result to var
			  var fieldContainsALowerCaseLetter = true;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
> 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
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);
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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...
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.
> is an associative array of regular expressions and associate user feedback messages

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

Thanks for all your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.