Regex for password verification in Javascript

The complexity of Regex Password requires applying any three following four characteristics when creating or changing a password.

  • Alpha characters - at least 1 uppercase alpha character
  • Alpha characters - At least 1 lowercase alpha character
  • Numeric characters - at least 1 numeric character
  • Special characters - at least 1 special character

I am trying to use the following code, but it does not work for special characters

 (?=^.{6,}$)((?=.*\d)(?=.*[AZ])(?=.*[az])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[az])|(?=.*[^A-Za-z0-9])(?=.*[AZ])(?=.*[az])|(?=.*\d)(?=.*[AZ])(?=.*[^A-Za-z0-9]))^.* 

I want my regular expression to be checked for the following 4 cases

Relevant Cases

  • P @ssword
  • Password1
  • p @ ssword1
  • p @ 12345
+6
source share
4 answers

I think that in this case one regular expression will be messy. You can easily do something like

 var count = 0; count += /[az]/.test(password) ? 1 : 0; count += /[AZ]/.test(password) ? 1 : 0; count += /\d/.test(password) ? 1 : 0; count += /[@]/.test(password) ? 1 : 0; if(count > 2) { alert('valid') } 
+6
source

I think you can use regex:

 (?=^.{6,}$)(?=.*[0-9])(?=.*[AZ])(?=.*[az])(?=.*[^A-Za-z0-9]).* 

I'm not sure why you have so many statements or statements in your regex, but this is appropriate if:

  • (?=^.{6,}$) - String> 5 characters
  • (?=.*[0-9]) - contains a digit
  • (?=.*[AZ]) - contains an uppercase letter
  • (?=.*[AZ]) - contains a lowercase letter
  • (?=.*[^A-Za-z0-9]) - the character is not alphanumeric.

Regular expression image

+3
source

Use this regex:

(? = ^. {6,10} $) (? =. \ D) (? =. [Az]) (? =. [AZ]) (? =. [! @ # $% ^ & * () _ +} {":;?.?! .." /> <,]) (\ s) $ **

+2
source

I think you will need this for all special characters: [updated to reject space]

  $(document).ready(function(){ $('input').change(function(){ var count = 0; var pass = $(this).val(); count += /[az]/.test(pass) ? 1 : 0; count += /[AZ]/.test(pass) ? 1 : 0; count += /\d/.test(pass) ? 1 : 0; count += /[^\w\d\s]/.test(pass) ? 1 : 0; (count>2 & !/[\s]+/.test(pass)) ? $(this).css('background-color','lime'):$(this).css('background-color','orange'); }); }); 

and fiddle: jsFiddle

0
source

Source: https://habr.com/ru/post/947279/


All Articles