How to accept only numeric numbers which has maximum 5 length for an input in Angular?

Discussão em 'Angular' iniciado por Kalana Tebel, Setembro 12, 2025.

  1. Kalana Tebel

    Kalana Tebel Guest

    I have a input field type number and I want to achieve the following scenario.

    1. Value length should be less than 5. (Max Length 5)
    2. If above condition true only, submit button will enable.

    Can someone help me on this?

    .ts

    public searchForm: FormGroup = new FormGroup({
    zip: new FormControl("",[Validators.required,Validators.pattern(/^-?(0|[1-9]\d*)?$/), Validators.maxLength(5)]),
    });


    .html

    <form [formGroup]="searchForm">
    <div class = "form-group">
    <input class="form-control form-text-input" type="number" name="zip"
    formControlName="zip" id="zip" placeholder="Enter a Zip Code" />
    </div>

    <button (click)="myFunction(myModal)" class="btn search-btn mb-1"
    type="button [disabled]="searchForm.invalid">Search
    </button>
    </form>

    Continue reading...

Compartilhe esta Página