Common User Information Form Validation

Form elements

this.validateForm = this.fb.group({
      account          : [ null, [ Validators.required, this.accountValidator ] ],//账户
      password         : [ null, [ Validators.required, this.passwordValidator ] ],//密码
      checkPassword    : [ null, [ Validators.required, this.confirmationValidator ] ],//确认密码
      name             : [ null, [ Validators.required, this.usernameValidator ] ],//用户名
      phoneNumber      : [ null, [ Validators.required, this.phoneValidator ] ],//手机号
    });

 

Account (English, numbers 3-10 bit)

// 账户校验
  public accountValidator = (control: FormControl): { [s: string]: boolean } => {
    const EMAIL_REGEXP = /^[a-zA-Z0-9]{3,10}$/;
    if (!control.value) {
      return { required: true };
    } else if (!EMAIL_REGEXP.test(control.value)) {
      return { confirm: true, error: true };
    }
  }

User Name (English 3-10)

public usernameValidator = (control: FormControl): { [s: string]: boolean => {
    const EMAIL_REGEXP = /^[\u2E80-\u9FFFa-zA-Z]{3,10}$/;
    if (!control.value) {
      return { required: true };
    } else if (!EMAIL_REGEXP.test(control.value)) {
      return { confirm: true, error: true };
    }
  }

Password (English, numbers, character 6-16)

public passwordValidator = (control: FormControl): { [s: string]: boolean } => {
    const EMAIL_REGEXP = /^[A-Za-z0-9#.$@!~%^&*]{6,16}$/;
    if (!control.value) {
      return { required: true };
    } else if (!EMAIL_REGEXP.test(control.value)) {
      return { confirm: true, error: true };
    }
  }

confirm password

public confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { required: true };
    } else if (control.value !== this.validateForm.controls[ 'password' ].value) {
      return { confirm: true, error: true };
    }
  }

 

Contact (11 phone number, landline)

public phoneValidator = (control: FormControl): { [s: string]: boolean } => {
    const EMAIL_REGEXP = /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/;
    if (!control.value) {
      return { required: true };
    } else if (!EMAIL_REGEXP.test(control.value)) {
      return { confirm: true, error: true };
    }
  }

Guess you like

Origin www.cnblogs.com/zhuangcui/p/12134637.html