3

I am new to angular js. I have created a login screen. I need to validate my password. it should contain one special character from -'$@£!%*#?&' and at least one letter and number. As of now it accepts all special characters without any limitations. I have 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);
}
1
  • Do you mean you want to limit the chars a user can use for a password? It is not best practice. You can achieve it with /^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[$@£!%*#?&])[A-Za-z0-9$@£!%*#?&]{8,}$/ regex. Commented Apr 12, 2017 at 10:41

1 Answer 1

2

Note that your current regex imposes 4 types of restriction:

  1. At least one ASCII letter ((?=.*?[A-Za-z])),
  2. At least one digit ((?=.*?[0-9])),
  3. At least one specific char from the set ((?=.*?[$@£!%*#?&]))
  4. The whole string should have at least 8 chars (.{8,})

The . in .{8,} can match any char other than line break chars.

If you plan to restrict the . and only allow users to type the chars from your sets, create a superset from 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 the regex demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.