3

I'm using this plugin within my Angular 2 project to mask some inputs.

The problem I have with this is to validate if the input has the proper mask applied.

Whenever I try to check for the value it's alwayas valid. For example, I have a mask to fill a phone number, just like in the example:

@Component({
  template: `
    <input [textMask]="{mask: mask}" type="tel" formControlName="phone" />
  `
})

export class AppComponent {
  public myModel = ''
  public mask = ['(', /[1-9]/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, /\d/, /\d/]
}

But even when I fill, let's say, 2 numbers (11_) ___-____ it's considering the field as valid.

I tried to se a custom Validator to check for the input length, but it's also passing, since it's being prefilled with _.

this.registerForm = this._fb.group({
    'phone': [null, Validators.compose([
        Validators.required,
        Validators.minLength(14),
        Validators.maxLength(14)
    ])],
});

I don't know how to create a validation for this type of scenario, envolving more complex validations.

2
  • you need to write a custom validation code to strip the masking characters and return true or false Commented Jul 1, 2017 at 19:10
  • 3
    The following should work as expected: Validators.pattern(/^\([1-9]\d{2}\)\s\d{3}-\d{4}$/); Test. Commented Jul 1, 2017 at 19:23

1 Answer 1

1

Just registering the correct answer from @developer033. It worked like a charm for me.

The following should work as expected: Validators.pattern(/^([1-9]\d{2})\s\d{3}-\d{4}$/);

Although I used a pattern that fit my needs.

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.