Validate zipcode to only allow length 5 or 7 digits in Angular 4
Asked Answered
S

4

7

I am using Angular 4. I want to allow the zipCode input field to only take an input of length 5 or 7 digits.

HTML code:

<md-input-container class="fill-width-input-wrap">
    <input mdInput placeholder="Zip Code" formControlName="zipCode" minLength="5" maxLength="7" (keypress)="allowOnlyNumbers($event)" required>
    <md-error *ngIf="clientInformationForm.controls['zipCode'].hasError('required')">
        Please enter
        <strong>valid zipcode</strong>
    </md-error>
    <md-error *ngIf="clientInformationForm.controls['zipCode'].hasError('minLength')">
        zip code
        <strong>minimum length is 5</strong>
    </md-error>
</md-input-container>
Sharpe answered 23/6, 2018 at 10:55 Comment(0)
C
4

I guess you want pattern attribute

<input mdInput formControlName="zipCode" 
       minLength="5" maxLength="7" 
       pattern="zipPattern" 
       (keypress)="allowOnlyNumbers($event)"
       required>
    <md-error *ngIf="clientInformationForm.controls['zipCode'].hasError('pattern')">
        zip code must satisfy pattern
    </md-error>
    ...

Where zipPattern is something like ^\d{5}(?:\d{2})?$:

const pattern = new RegExp(/^\d{5}(?:\d{2})?$/)
'1234'.match(pattern) // null
'12345'.match(pattern) // ["12345"
'123456'.match(pattern) // null
'1234567'.match(pattern) // ["1234567"
'12345678'.match(pattern) // null
Cockeye answered 23/6, 2018 at 11:24 Comment(0)
I
4

Just use default Pattern Validator

HTML :

<form class="form" [formGroup]="settings_form">    
   <input type="number" placeholder="Code postal" formControlName="postalcode"/>
</form>

Component.ts :

public settings_form : FormGroup
ngOnInit() {

        this.settings_form = this.formBuilder.group({
          postalcode: new FormControl('', Validators.compose([
                                   Validators.required,
                                   Validators.pattern('[0-9]{5}')
                                   )),    
        })
       }

And here are my imports :

import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';
Indistinctive answered 4/12, 2019 at 20:37 Comment(1)
the pattern validator does take a string but also can take a RegExp object itself. I think you need the slashes either way. I just use a regular expression itself: Validators.pattern(/[0-9]{5}/)Firedamp
C
1

Zip code validation

<input mdInput formControlName="zipCode" 
       minLength="5" maxLength="7" 
       pattern="zipPattern" 
       (keypress)="allowOnlyNumbers($event)"
       required>
    <md-error *ngIf="clientInformationForm.controls['zipCode'].hasError('pattern')">
        zip code must satisfy pattern
    </md-error>

zipPattern = /^[a-zA-Z0-9\s]{0,10}$/;

Note:zip code available on string ex: PO14 1AS UK zip code

Clockwise answered 22/11, 2019 at 4:54 Comment(0)
O
0

You can simply follow the below that i have followed for Zipcode field form numeric pattern for maxlength and maxlength:

 <input matInput [(ngModel)]="data.loc.zipcode" minlength="2" maxlength="6" pattern="[0-9]*" [errorStateMatcher]="customErrorStateMatcher" formControlName="zipcode" required>
Oaten answered 12/3, 2021 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.