Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase [duplicate]

I am trying to write a regular expression to validate a password which must meet the following criteria:

  • Contain at least 8 characters
  • contain at least 1 number
  • contain at least 1 lowercase character (a-z)
  • contain at least 1 uppercase character (A-Z)
  • contains only 0-9a-zA-Z

I tried the following but it doesn't seem to work.

http://jsfiddle.net/many_tentacles/Hzuc9/

<input type='button' value='click' class='buttonClick' /> <input type='text' /> <div></div> 

and...

$(".buttonClick").click(function () {      if ($("input[type=text]").filter(function () {         return this.value.match(/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])([a-zA-Z0-9]{8})$/);     })) {         $("div").text("pass");     } else {         $("div").text("fail");     }  }); 

Any ideas?

like image 289
Tom Avatar asked Feb 13 '13 09:02

Tom


People also ask

What does * regular expression indicate?

A regular expression is an object that describes a pattern of characters. The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.


2 Answers

Your regular expression should look like:

/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/ 

Here is an explanation:

/^   (?=.*\d)          // should contain at least one digit   (?=.*[a-z])       // should contain at least one lower case   (?=.*[A-Z])       // should contain at least one upper case   [a-zA-Z0-9]{8,}   // should contain at least 8 from the mentioned characters $/ 
like image 98
Minko Gechev Avatar answered Sep 19 '22 10:09

Minko Gechev


Using individual regular expressions to test the different parts would be considerably easier than trying to get one single regular expression to cover all of them. It also makes it easier to add or remove validation criteria.

Note, also, that your usage of .filter() was incorrect; it will always return a jQuery object (which is considered truthy in JavaScript). Personally, I'd use an .each() loop to iterate over all of the inputs, and report individual pass/fail statuses. Something like the below:

$(".buttonClick").click(function () {      $("input[type=text]").each(function () {         var validated =  true;         if(this.value.length < 8)             validated = false;         if(!/\d/.test(this.value))             validated = false;         if(!/[a-z]/.test(this.value))             validated = false;         if(!/[A-Z]/.test(this.value))             validated = false;         if(/[^0-9a-zA-Z]/.test(this.value))             validated = false;         $('div').text(validated ? "pass" : "fail");         // use DOM traversal to select the correct div for this input above     }); }); 

Working demo

like image 22
Anthony Grist Avatar answered Sep 20 '22 10:09

Anthony Grist