内置验证器
Validators.required
Validators.minLength(2)
Validators.maxLength(2)
const PHONE_NUMBER_REGEXP = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/
this.employeeEditForm = this.fb.group({
phoneNumber: ['', Validators.pattern(PHONE_NUMBER_REGEXP)]
})
自定义验证器
testForm: FormGroup
ngOnInit() {
this.fetchData()
this.testForm= this.fb.group({
joinDate: ['', this.joinDateValidate]
})
}
joinDateValidate(control: FormControl) {
const selectDate = +control.value
const curDate = +new Date()
if (selectDate > curDate) {
return { date: true }
}
return null
}