Skip to content

Commit 63978a0

Browse files
author
Mavlarn
committed
add custom validator
1 parent fcd400d commit 63978a0

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed

app/app.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import 'rxjs/add/operator/distinctUntilChanged';
1010
import { AppComponent } from './app.component';
1111
import { TemplateFormsComponent } from './template-forms/template-forms.component';
1212
import { ReactiveFormsComponent } from './reactive-forms/reactive-forms.component';
13+
import { MobileValidator } from './validators/mobile.validator';
1314
import { routes } from './app.routes';
1415

1516
@NgModule({
1617
imports: [BrowserModule, FormsModule, ReactiveFormsModule, RouterModule.forRoot(routes)],
17-
declarations: [AppComponent, TemplateFormsComponent, ReactiveFormsComponent],
18+
declarations: [AppComponent, TemplateFormsComponent, ReactiveFormsComponent, MobileValidator],
1819
bootstrap: [AppComponent]
1920
})
2021
export class AppModule {}

app/reactive-forms/reactive-forms.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<div [hidden]="userForm.controls.mobile.valid || userForm.controls.mobile.pristine" class="alert alert-danger">
2929
<p *ngIf="userForm.controls.mobile.errors?.minlength || userForm.controls.mobile.errors?.maxlength">电话长度必须为11</p>
3030
<p *ngIf="userForm.controls.mobile.errors?.required">必须输入电话</p>
31+
<p *ngIf="userForm.controls.mobile.errors?.validateMobile">电话号码格式不正确</p>
3132
</div>
3233
</div>
3334
</div>

app/reactive-forms/reactive-forms.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Component, OnInit } from '@angular/core';
22
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
33

4+
import { validateMobile } from '../validators/mobile.validator';
5+
46
@Component({
57
selector: 'reactive-form',
68
templateUrl: 'app/reactive-forms/reactive-forms.component.html',
@@ -16,7 +18,7 @@ export class ReactiveFormsComponent implements OnInit {
1618
ngOnInit() {
1719
this.userForm = this.formBuilder.group({
1820
name: ['张三', [Validators.required, Validators.minLength(3)]],
19-
mobile: [13800138001, [Validators.required, Validators.minLength(11), Validators.maxLength(11)]],
21+
mobile: [13800138001, [Validators.required, Validators.minLength(11), Validators.maxLength(11), validateMobile]],
2022
address: this.formBuilder.group({
2123
city: ['北京', Validators.required],
2224
street: ['朝阳望京...', Validators.required]

app/template-forms/template-forms.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@
2323
<div class="form-group">
2424
<label class="col-sm-2 control-label">电话:</label>
2525
<div class="col-sm-10">
26-
<input class="form-control" type="text" name="mobile" [ngModel]="user.mobile" required minlength="11" maxlength="11">
26+
<input class="form-control" type="text" name="mobile" [ngModel]="user.mobile" required minlength="11" maxlength="11" validateMobile>
2727
<span *ngIf="userForm.controls.mobile?.pristine" class="label label-primary">未修改</span>
2828
<span *ngIf="userForm.controls.mobile?.dirty" class="label label-warning">已修改</span>
2929
<span *ngIf="userForm.controls.mobile?.valid" class="label label-success">有效</span>
3030
<div [hidden]="userForm.controls.mobile?.valid || userForm.controls.mobile?.pristine" class="alert alert-danger">
3131
<p *ngIf="userForm.controls.mobile?.errors?.minlength">电话长度必须为11</p>
3232
<p *ngIf="userForm.controls.mobile?.errors?.required">必须输入电话</p>
33+
<p *ngIf="userForm.controls.mobile?.errors?.validateMobile">电话号码格式不正确</p>
3334
</div>
3435
</div>
3536
</div>

app/template-forms/template-forms.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Component } from '@angular/core';
22
import {NgForm} from '@angular/forms';
33

4+
import { MobileValidator } from '../validators/mobile.validator';
5+
46
@Component({
57
selector: 'template-form',
68
templateUrl: 'app/template-forms/template-forms.component.html',

app/validators/mobile.validator.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { FormControl, NG_VALIDATORS } from '@angular/forms';
2+
import { Directive } from '@angular/core';
3+
4+
export function validateMobile(c: FormControl) {
5+
let MOBILE_REGEXP = /^1[0-9]{10,10}$/;
6+
7+
return MOBILE_REGEXP.test(c.value) ? null : {
8+
validateMobile: {valid: false}
9+
}
10+
}
11+
12+
@Directive({
13+
selector: '[validateMobile][ngModel]',
14+
providers: [
15+
{ provide: NG_VALIDATORS, useValue: validateMobile, multi: true }
16+
]
17+
})
18+
export class MobileValidator {}

0 commit comments

Comments
 (0)