Angularjs regex password validation

I am new to angular js. I created a login screen. I need to check my password. it must contain one special character from -'$@£!%*#?&'

and at least one letter and number. For now, it accepts all special characters without any restrictions. I have the following code

if (vm.newpassword_details.password.search("^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[$@£!%*#?&]).{8,}$")) {
  var msg = "Password should contain one special character from -'$@£!%*#?&' and at least one letter and number";
  alert(msg);
}

      

+3


source to share


1 answer


Note that your current regex imposes 4 types of constraints:

  • At least one ASCII letter ( (?=.*?[A-Za-z])

    ),
  • At least one digit ( (?=.*?[0-9])

    ),
  • At least one specific char from set ( (?=.*?[$@£!%*#?&])

    )
  • The whole line must contain at least 8 characters ( .{8,}

    )

.

c .{8,}

can match any char other than line breaks.



If you plan to restrict .

and allow users to enter characters from your sets, create a superset above them and use it with RegExp#test

:

if (!/^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[$@£!%*#?&])[A-Za-z0-9$@£!%*#?&]{8,}$/.test(vm.newpassword_details.password)) {  /* Error ! */  }

      

See regex demo

+1


source







All Articles