内置验证器

// 必填
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
    // console.log(selectDate)
    const curDate = +new Date()

    if (selectDate > curDate) {
      return { date: true }
    }

    return null
  }