0
How to apply a phone mask where it adapts to a landline and mobile phone, using the Angular2-text-mask
?
I read the documentation and saw that you can do with function, but as I am using formGroup
, I don’t know how to take the value of my field...
// Mascaras
cpfMask = [/\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '-', /\d/, /\d/];
cepMask = [/\d/, /\d/, /\d/, /\d/ , /\d/, '-', /\d/, /\d/, /\d/];
constructor(fb: FormBuilder, public router: Router, private vwServicePagination: VwServicePagination){
//Grupo de formulario - Validações
this.myForm = fb.group({
nome: new FormControl('', [ Validators.required, Validators.minLength(2), this.validaNome ]),
email: new FormControl('', [ Validators.required, Validators.pattern(this.validaEmail) ]),
cpf: new FormControl('', [ Validators.required, this.validaCpf ]),
telefone: new FormControl('', [ Validators.required ]),
cep: new FormControl('', [ Validators.required, Validators.pattern(this.validaCep) ])
});
}
Masquerade...
// Mascaras
cpfMask = [/\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '.', /\d/, /\d/, /\d/, '-', /\d/, /\d/];
cepMask = [/\d/, /\d/, /\d/, /\d/ , /\d/, '-', /\d/, /\d/, /\d/];
telefoneMask = function(){
if(this.myForm.controls.telefone.value.length === 9){
[/\d/, /\d/, /\d/, /\d/ , /\d/, '-', /\d/, /\d/, /\d/, /\d/];
}else{
[/\d/, /\d/, /\d/, /\d/ , '-', /\d/, /\d/, /\d/, /\d/, /\d/];
}
}
constructor(fb: FormBuilder, public router: Router, private vwServicePagination: VwServicePagination){
//Grupo de formulario - Validações
this.myForm = fb.group({
nome: new FormControl('', [ Validators.required, Validators.minLength(2), this.validaNome ]),
email: new FormControl('', [ Validators.required, Validators.pattern(this.validaEmail) ]),
cpf: new FormControl('', [ Validators.required, this.validaCpf ]),
telefone: new FormControl('', [ Validators.required ]),
cep: new FormControl('', [ Validators.required, Validators.pattern(this.validaCep) ])
});
}
I tried to use this way but it did not work!! I entered the code of my attempt in the question, because here in the comments has character limit...
– Guilherme Nunes
Eita o value changes e um observable que vc poe no onInit e acho que não é só mudar o valor da mascara! You have to remove Validator and add the right.
– Eduardo Vargas
https://scotch.io/tutorials/how-to-implement-conditional-validation-in-angular-2-model-driven-forms
– Eduardo Vargas
That way it didn’t roll either! : / I have to find a way to use 'Angular2-text-Mask'
– Guilherme Nunes