admin管理员组文章数量:1022980
I have created a student form that has 4 to 5 fields. For the contact number, I have 2 fields, country code and phone number. I want to throw an error if the user only inputs one of the two fields ie country code is entered but the phone number isn't or vice-versa. However, if both fields are left empty, then it's valid as both are non-mandatory fields. I don't know how I can achieve this. Please help. Thanks in advance.
Here is my template:
<mat-form-field >
<input matInput type="number" formControlName="countryCode" placeholder="Country Code(+)" name="countryCode" class="form-control" id="countryCode">
</mat-form-field>
<mat-form-field >
<input matInput type="number" formControlName="phnNumber" placeholder="Phone Number" name="phnNumber" class="form-control" id="phnNumber">
</mat-form-field>
My Form group:
this.addForm = this.formBuilder.group({
studentName: ['', Validators.required],
phnNumber: new FormControl(''),
countryCode: new FormControl(''),
});
I have created a student form that has 4 to 5 fields. For the contact number, I have 2 fields, country code and phone number. I want to throw an error if the user only inputs one of the two fields ie country code is entered but the phone number isn't or vice-versa. However, if both fields are left empty, then it's valid as both are non-mandatory fields. I don't know how I can achieve this. Please help. Thanks in advance.
Here is my template:
<mat-form-field >
<input matInput type="number" formControlName="countryCode" placeholder="Country Code(+)" name="countryCode" class="form-control" id="countryCode">
</mat-form-field>
<mat-form-field >
<input matInput type="number" formControlName="phnNumber" placeholder="Phone Number" name="phnNumber" class="form-control" id="phnNumber">
</mat-form-field>
My Form group:
this.addForm = this.formBuilder.group({
studentName: ['', Validators.required],
phnNumber: new FormControl(''),
countryCode: new FormControl(''),
});
Share
Improve this question
edited Aug 22, 2019 at 12:07
Taimoor Suleman
1,62617 silver badges30 bronze badges
asked Aug 22, 2019 at 11:42
rock11rock11
7585 gold badges19 silver badges38 bronze badges
1 Answer
Reset to default 5You can create a custom validator which you place on the FormGroup
as a whole rather than the individual controls. Your call to FormBuilder
bees:
this.addForm = this.formBuilder.group({
studentName: ['', Validators.required],
phnNumber: new FormControl(''),
countryCode: new FormControl(''),
}, { validators: myFormValidator });
And you create a Validator (either in the same file or elsewhere and export):
export const myFormValidator: ValidatorFn = (control: FormGroup): ValidationErrors | null => {
const phnNumber = control.get('phnNumber').value;
const countryCode = control.get('countryCode').value;
if (phnNumber && !countryCode || countryCode && !phnNumber) {
return { 'phoneNumberError': true };
} else {
return null;
}
};
That way the custom Validator grabs what it needs from the whole group as opposed to needing to validate controls individually.
I have created a student form that has 4 to 5 fields. For the contact number, I have 2 fields, country code and phone number. I want to throw an error if the user only inputs one of the two fields ie country code is entered but the phone number isn't or vice-versa. However, if both fields are left empty, then it's valid as both are non-mandatory fields. I don't know how I can achieve this. Please help. Thanks in advance.
Here is my template:
<mat-form-field >
<input matInput type="number" formControlName="countryCode" placeholder="Country Code(+)" name="countryCode" class="form-control" id="countryCode">
</mat-form-field>
<mat-form-field >
<input matInput type="number" formControlName="phnNumber" placeholder="Phone Number" name="phnNumber" class="form-control" id="phnNumber">
</mat-form-field>
My Form group:
this.addForm = this.formBuilder.group({
studentName: ['', Validators.required],
phnNumber: new FormControl(''),
countryCode: new FormControl(''),
});
I have created a student form that has 4 to 5 fields. For the contact number, I have 2 fields, country code and phone number. I want to throw an error if the user only inputs one of the two fields ie country code is entered but the phone number isn't or vice-versa. However, if both fields are left empty, then it's valid as both are non-mandatory fields. I don't know how I can achieve this. Please help. Thanks in advance.
Here is my template:
<mat-form-field >
<input matInput type="number" formControlName="countryCode" placeholder="Country Code(+)" name="countryCode" class="form-control" id="countryCode">
</mat-form-field>
<mat-form-field >
<input matInput type="number" formControlName="phnNumber" placeholder="Phone Number" name="phnNumber" class="form-control" id="phnNumber">
</mat-form-field>
My Form group:
this.addForm = this.formBuilder.group({
studentName: ['', Validators.required],
phnNumber: new FormControl(''),
countryCode: new FormControl(''),
});
Share
Improve this question
edited Aug 22, 2019 at 12:07
Taimoor Suleman
1,62617 silver badges30 bronze badges
asked Aug 22, 2019 at 11:42
rock11rock11
7585 gold badges19 silver badges38 bronze badges
1 Answer
Reset to default 5You can create a custom validator which you place on the FormGroup
as a whole rather than the individual controls. Your call to FormBuilder
bees:
this.addForm = this.formBuilder.group({
studentName: ['', Validators.required],
phnNumber: new FormControl(''),
countryCode: new FormControl(''),
}, { validators: myFormValidator });
And you create a Validator (either in the same file or elsewhere and export):
export const myFormValidator: ValidatorFn = (control: FormGroup): ValidationErrors | null => {
const phnNumber = control.get('phnNumber').value;
const countryCode = control.get('countryCode').value;
if (phnNumber && !countryCode || countryCode && !phnNumber) {
return { 'phoneNumberError': true };
} else {
return null;
}
};
That way the custom Validator grabs what it needs from the whole group as opposed to needing to validate controls individually.
本文标签: javascriptangular validate that a pair of form fields are empty or both are filledStack Overflow
版权声明:本文标题:javascript - angular validate that a pair of form fields are empty or both are filled - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745558949a2156043.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论