zip validation
This commit is contained in:
parent
b6278dfe2f
commit
18598ddfe2
3 changed files with 34 additions and 7 deletions
|
@ -76,6 +76,12 @@
|
|||
formControlName="zip"
|
||||
type="text"
|
||||
/>
|
||||
@if (
|
||||
addressGroup.controls["zip"].errors &&
|
||||
addressGroup.controls["zip"].touched
|
||||
) {
|
||||
<p style="color: red">Zip is invalid</p>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -18,10 +18,15 @@ export default class RegisterComponent {
|
|||
|
||||
fb: FormBuilder = inject(FormBuilder);
|
||||
|
||||
emailGroup: FormGroup = this.fb.group({
|
||||
email: ['', [Validators.required, Validators.email]],
|
||||
confirmEmail: ['', [Validators.required, Validators.email]],
|
||||
});
|
||||
emailGroup: FormGroup = this.fb.group(
|
||||
{
|
||||
email: ['', [Validators.required, Validators.email]],
|
||||
confirmEmail: ['', [Validators.required, Validators.email]],
|
||||
},
|
||||
{
|
||||
validators: CustomValidators.email(),
|
||||
},
|
||||
);
|
||||
|
||||
passwordGroup: FormGroup = this.fb.group(
|
||||
{
|
||||
|
@ -36,7 +41,7 @@ export default class RegisterComponent {
|
|||
addressGroup: FormGroup = this.fb.group({
|
||||
city: ['', Validators.required],
|
||||
state: ['', Validators.required],
|
||||
zip: ['', Validators.required],
|
||||
zip: ['', Validators.required, CustomValidators.zipCode()],
|
||||
street: ['', Validators.required],
|
||||
nr: ['', Validators.required],
|
||||
});
|
||||
|
@ -51,7 +56,7 @@ export default class RegisterComponent {
|
|||
|
||||
submit() {
|
||||
if (!this.form.valid) {
|
||||
console.log(this.form.controls['password'].errors);
|
||||
console.log(this.addressGroup.controls['zip']);
|
||||
console.log(this.form.value);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { AbstractControl, ValidatorFn } from '@angular/forms';
|
||||
import { AbstractControl, AsyncValidatorFn, ValidatorFn } from '@angular/forms';
|
||||
|
||||
export default class CustomValidators {
|
||||
public static email(): ValidatorFn {
|
||||
|
@ -11,6 +11,22 @@ export default class CustomValidators {
|
|||
};
|
||||
}
|
||||
|
||||
public static zipCode(): AsyncValidatorFn {
|
||||
return async (zip: AbstractControl) => {
|
||||
if (String(zip.value).length != 5) {
|
||||
return { zipInvalid: true };
|
||||
}
|
||||
|
||||
const response = await fetch('https://zippopotam.us/DE/' + zip.value);
|
||||
|
||||
if (false == response.ok) {
|
||||
return { zipInvalid: true };
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
}
|
||||
|
||||
public static password(): ValidatorFn {
|
||||
return (passwordGroup: AbstractControl) => {
|
||||
console.log(passwordGroup);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue