Angular 2 form validation, minLength validator is not working

前端 未结 7 1746
旧巷少年郎
旧巷少年郎 2021-02-06 21:04

I have following Angular 2 form:


    
&l
7条回答
  •  自闭症患者
    2021-02-06 21:24

    I had the same problem where I wanted to validate a number field that contained the year, so I used Validators.maxLength(4) to make sure someone does not accidently type 5 numbers for a year. Turns out when you set the input type to number:

    
    

    then maxLength does not "work" anymore. Which actually makes sense if you think about it for half a second.

    So I changed my date input to:

    year: [null, [Validators.required, Validators.min(1990), Validators.max(2050)]],
    

    Which is the right approach when working with a number field.

提交回复
热议问题