Angular 4 Form Validators - minLength & maxLength does not work on field type number

前端 未结 12 961
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 18:06

I am trying to develop a contact form, I want user to enter phone number values between length 10-12.

Notably same validation is working on Message

12条回答
  •  时光说笑
    2021-01-03 18:38

    try this working sample code :

    component.html

    Your phone must be at least 5 characters long.

    Your phone cannot exceed 10 characters.

    phone is required

    component.ts

    import { FormGroup, FormBuilder, Validators } from '@angular/forms';
    
    export class AppComponent implements OnInit {
    myForm: any;
    constructor(
            private formBuilder: FormBuilder
        ) {}
    
    ngOnInit() {
        this.myForm = this.formBuilder.group({
                phone: [null, Validators.compose([Validators.required, Validators.minLength(5), Validators.maxLength(10)])]
            });
    }
    }
    

提交回复
热议问题