I have realized form validation in accordance with https://auth0.com/blog/angular-2-series-part-5-forms-and-custom-validation/
<input class="form-control"
type="text"
name="phone"
autocomplete="off"
placeholder="(XXX)-XXX-XXXX"
mask=""
[disabled]="disabled"
[(ngModel)]="candidate.phone"
ngControl="phone"/>
...
...
static phone(control: Control): ValidationResult {
let URL_REGEXP = /^\(\d{3}\)-\d{3}-\d{4}$/i;
if (control.value && (control.value.length <= 5 || !URL_REGEXP.test(control.value))) {
return {"phone": true};
}
return null;
}
plus for this element I have realized the input mask directory: http://pastebin.com/wRzHSsVy
The following problem occurs: when a telephone number is entered the validation acts first and then the input mask directory. So the data checked by the validator and the data formated by the input mask directory differ. For instance, the phone number on the validator is (888)-888-88882 and the mask turns the number to the following format (888)-888-8888, but the validator has already worked and pointed the mistake before the mask activation.
(ngModelChange)
and(keydown.backspace)
. The backspace was to handle the masking changes so that backspace would delete numbers on each stroke. Here it is: https://mcmap.net/q/268439/-mask-for-an-input-to-allow-phone-numbers – Anticipate