Angular实现表单验证功能(2)
app.component.css
*{ font-size: 18px; } .wrapper{ margin: 0 auto; margin-top:10%; width:30%; height: 20%; border:1px solid black; border-radius: 10px; } .title-wrapper{ margin: 0 auto; padding-top: 20px; padding-bottom: 20px; width:370px; text-align: center; font-size: 20px; font-weight: 800; } label{ display: inline-block; width:72px; } .contain-wrapper{ width: 300px; margin:0 auto; } .confirm{ margin-top:20px; width:100%; }
3.项目效果
二、自定义验证
自定义表单验证,需要创建自定义验证器,我们接下来更改邮箱的验证,将其改为有格式的验证,而不是单纯的存在验证,首先我们来看一下项目目录的更改
1.项目目录
----------app.component.ts
----------app.component.html
----------app.component.css
----------app.module.ts
----------emailAuthentication.ts
2.项目代码
app.module.ts
注册自定义验证器EmailValidatorDirective
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule,ReactiveFormsModule } from '@angular/forms'; import { EmailValidatorDirective } from './emailAuthentication'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent, EmailValidatorDirective ], imports: [ BrowserModule, FormsModule, ReactiveFormsModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
emailAuthentication.ts
import { Directive, Input, OnChanges, SimpleChanges } from '@angular/core'; import { AbstractControl, NG_VALIDATORS, Validator, ValidatorFn, Validators } from '@angular/forms'; /** A hero's name can't match the given regular expression */ export function emailValidator(nameRe: RegExp): ValidatorFn { return (control: AbstractControl): { [key: string]: any } => { const forbidden = !nameRe.test(control.value); return forbidden ? { 'forbiddenName': { value: control.value } } : null; }; } @Directive({ selector: '[appForbiddenName]', providers: [{ provide: NG_VALIDATORS, useExisting: EmailValidatorDirective, multi: true }] }) export class EmailValidatorDirective implements Validator { @Input() forbiddenName: string; validate(control: AbstractControl): { [key: string]: any } { return this.forbiddenName ? emailValidator(new RegExp(this.forbiddenName, 'i'))(control) : null; } }
app.component.ts
import { Component,OnInit } from '@angular/core'; import { FormGroup, FormControl, Validators } from '@angular/forms'; import { emailValidator } from './emailAuthentication'; //导入emailValidator自定义验证器 @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'app'; //email的正则表达式 emailExp = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/ ; Form:FormGroup; data={ name:"", email:"", tel:"" } ngOnInit(): void { this.Form = new FormGroup({ 'name': new FormControl(this.data.name, [ Validators.required, Validators.minLength(4) ]), 'email': new FormControl(this.data.email, [ Validators.required, emailValidator(this.emailExp) //自定义验证器 ]), 'tel': new FormControl(this.data.tel, Validators.required) }); } get name() { return this.Form.get('name'); } get email() { return this.Form.get('email'); } get tel() { return this.Form.get('tel'); } }
内容版权声明:除非注明,否则皆为本站原创文章。