Hellow, i'm try to use a validator for an IP address in angular. However, it seems that even if i inputted an invalid ip address i.e 12.2.2.2..., GUI shows it is valid (see pic). however, console logs shows that it is a pattern mistmatch (this means that it detected an incorrect pattern). I am not sure what i am missing. starting to get confused now. Need a new pair of eyes to spot the defect.
Please note that the required part is already working with ipaddress. I am just confused as to whay it isn't working on pattern validation. See image
See my stackblitz
Here is my code
HTML
<div class="form-group row required" [ngClass]="{
'is-invalid': ipaddress.invalid && (ipaddress.dirty || ipaddress.touched),
'is-valid': ipaddress.valid && (ipaddress.dirty || ipaddress.touched)
}">
<label for="ipaddress" class="col-sm-3 col-form-label">IP Address</label>
<div class="col-sm-7">
<input type="text" class="form-control" placeholder="IP Address"
id="ipaddress" name="ipaddress" formControlName="ipaddress"
ngbAutofocus required>
<div class="form-control-feedback invalid-feedback"
*ngIf="ipaddress.errors">
<p *ngIf="ipaddress.errors.required">IP Address is required</p>
<p *ngIf="ipaddress.errors.pattern">Invalid IP Address format</p>
</div>
</div>
</div>
TS File
this.ipaddress = new FormControl('', {
validators: Validators.compose([
Validators.required,
Validators.pattern('(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)')
])
});
