Geoff
Geoff

Reputation: 6649

Angular2 password validation with special characters

I would like to setup validation for password field to contain:

Both capital letters, small letters, numbers and special characters

This is what I have so far:

passwordValueValidator(control) {
  if (control.value != undefined) {
    if (!control.value.match(/^(?=.*[0-9])[a-zA-Z0-9!@#$%^&*]{6,100}$/)) {
      return { 'invalidPassword': true };
    }
    else{
      //here i need to add check for special characters
    }
  }
}

The above code works with only letters and numbers combinations. What else do I need to add to also check if a user has entered the special characters

Special characters are: !@#$%^&*()_+ (enterd by shift + numbers 0-10)

Upvotes: 1

Views: 5654

Answers (1)

Yordan Nikolov
Yordan Nikolov

Reputation: 2678

Try this one: /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{6,100})/

Upvotes: 6

Related Questions