0

I want some specific validations in a form and I cannot find an elegant way of doing so. Let's say I have a field, that can satisfy one of a few groups of validation constraints to be valid? The specific validation for a password I want is:

  • required
  • max length 256 characters
  • min length 10 characters
  • Requires at least one integer
  • Requires at least one symbol
  • Requires at least one capital letter

OR

  • required
  • max length 256 characters
  • min length 32 characters

I want validation to accept either of these groups for the same field. Is there an elegant way to do this with angular forms?

FatalCatharsis
  • 3,407
  • 5
  • 44
  • 74
  • what i am guessing is that you want a validator that has all the validation and you can plug and play the validations you want ? if this is the case you can have a different class for your validator and then use the same function call in your formbuilder – Rahul Singh May 12 '17 at 07:28

1 Answers1

0

you can set validation like that. and you can set pattern for numeric, symbol and alpha

   this.userForm = this.formBuilder.group({
    password: ['', [Validators.required, Validators.maxLength(256)], Validators.minLength(10) , Validators.pattern('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{10,}$')],
    });

pattern found from here

Community
  • 1
  • 1
Shailesh Ladumor
  • 7,052
  • 5
  • 42
  • 55