欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular實(shí)現(xiàn)表單驗(yàn)證功能

 更新時(shí)間:2017年11月13日 14:26:35   作者:fyk曩昔  
這篇文章主要為大家詳細(xì)介紹了Angular實(shí)現(xiàn)表單驗(yàn)證功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Angular表單驗(yàn)證分為兩種驗(yàn)證:1.內(nèi)置驗(yàn)證(required,minlength等);2.自定義驗(yàn)證(正則表達(dá)式)。

接下來(lái)我們用一個(gè)注冊(cè)賬號(hào)的demo來(lái)看一下這兩種驗(yàn)證是如何實(shí)現(xiàn)的。

項(xiàng)目界面

一、內(nèi)置驗(yàn)證

其中賬戶名有required驗(yàn)證和最短長(zhǎng)度驗(yàn)證,其他兩個(gè)只有required驗(yàn)證

1.項(xiàng)目目錄

----------app.component.ts

----------app.component.html

----------app.component.css

----------app.module.ts

2.項(xiàng)目代碼

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule,ReactiveFormsModule }  from '@angular/forms';//表單驗(yàn)證必須導(dǎo)入這兩個(gè)模塊

import { AppComponent } from './app.component';

@NgModule({
 declarations: [
  AppComponent
 ],
 imports: [
  BrowserModule,
  FormsModule,  //注冊(cè)模塊
  ReactiveFormsModule
 ],
 providers: [],
 bootstrap: [AppComponent]
})
export class AppModule { } 

app.component.ts

import { Component,OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent {
 title = 'app';
 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),
      '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'); }
}

簡(jiǎn)單來(lái)說(shuō),在使用驗(yàn)證表單的時(shí)候,大致分為四步:

(1)導(dǎo)入相關(guān)模塊FormGroup, FormControl, Validators;

(2)聲明表單驗(yàn)證變量From:FromGroup;

(3)定義驗(yàn)證規(guī)則;

(4)通過(guò)它所屬的控件組(FormGroup)的get方法來(lái)訪問(wèn)表單控件

app.component.html

<div class="wrapper">
  <div class="row">
    <p class="title-wrapper">注冊(cè)賬號(hào)</p>
  </div>
  <div class="row">
    <div class="contain-wrapper" [formGroup]="Form">
      <label for="name">賬戶名:</label>
      <input type="text" id="name" formControlName="name"><br/>
      <div *ngIf="name.invalid && (name.dirty || name.touched)" class="alert alert-danger">
        <div *ngIf="name.errors.required">
          請(qǐng)輸入長(zhǎng)度賬戶名!
        </div>
        <div *ngIf="name.errors.minlength">
          賬戶名長(zhǎng)度不小于4!
        </div>
      </div>
      <label for="email">郵箱:</label>
      <input type="text" id="email" formControlName="email"><br/>
      <div *ngIf="email.invalid && (email.dirty || email.touched)" class="alert alert-danger">
        <div *ngIf="email.errors.required">
          請(qǐng)輸入郵箱!
        </div>
      </div>
      <label for="tel">電話:</label>
      <input type="text" id="tel" formControlName="tel">
      <div *ngIf="tel.invalid && (tel.dirty || tel.touched)" class="alert alert-danger">
        <div *ngIf="tel.errors.required">
          請(qǐng)輸入電話!
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <button class="btn btn-primary confirm">確認(rèn)</button>
  </div>
</div>

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.項(xiàng)目效果

二、自定義驗(yàn)證

自定義表單驗(yàn)證,需要?jiǎng)?chuàng)建自定義驗(yàn)證器,我們接下來(lái)更改郵箱的驗(yàn)證,將其改為有格式的驗(yàn)證,而不是單純的存在驗(yàn)證,首先我們來(lái)看一下項(xiàng)目目錄的更改

1.項(xiàng)目目錄

----------app.component.ts

----------app.component.html

----------app.component.css

----------app.module.ts

----------emailAuthentication.ts

2.項(xiàng)目代碼

app.module.ts

注冊(cè)自定義驗(yàn)證器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'; //導(dǎo)入emailValidator自定義驗(yàn)證器

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.css']
})
export class AppComponent {
 title = 'app';
 //email的正則表達(dá)式
 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) //自定義驗(yàn)證器
        ]),
      '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'); }
}

app.component.html

<div class="wrapper">
  <div class="row">
    <p class="title-wrapper">注冊(cè)賬號(hào)</p>
  </div>
  <div class="row">
    <div class="contain-wrapper" [formGroup]="Form">
      <label for="name">賬戶名:</label>
      <input type="text" id="name" formControlName="name"><br/>
      <div *ngIf="name.invalid && (name.dirty || name.touched)" class="alert alert-danger">
        <div *ngIf="name.errors.required">
          請(qǐng)輸入賬戶名!
        </div>
        <div *ngIf="name.errors.minlength">
          賬戶名長(zhǎng)度不小于4!
        </div>
      </div>
      <label for="email">郵箱:</label>
      <input type="text" id="email" formControlName="email" required><br/>
      <div *ngIf="email.invalid && (email.dirty || email.touched)" class="alert alert-danger">
        <div *ngIf="email.errors.forbiddenName">
         請(qǐng)輸入正確格式的郵箱!
        </div>
      </div>
      <label for="tel">電話:</label>
      <input type="text" id="tel" formControlName="tel">
      <div *ngIf="tel.invalid && (tel.dirty || tel.touched)" class="alert alert-danger">
        <div *ngIf="tel.errors.required">
          請(qǐng)輸入電話!
        </div>
      </div>
    </div>
  </div>
  <div class="row">
    <button class="btn btn-primary confirm" [disabled]="Form.invalid" >確認(rèn)</button>
  </div>
</div>

在最后確認(rèn)的時(shí)候,我們?cè)O(shè)置一下按鈕的disabled屬性,在表單驗(yàn)證不通過(guò)的時(shí)候,確認(rèn)按鈕是點(diǎn)擊不了的,顯示不可點(diǎn)擊狀態(tài)。[disabled]="Form.invalid"。

3.項(xiàng)目效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例

    angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例

    這篇文章主要介紹了angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 詳解Angular-Cli中引用第三方庫(kù)

    詳解Angular-Cli中引用第三方庫(kù)

    本篇文章主要介紹了詳解Angular-Cli中引用第三方庫(kù) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • 關(guān)于使用axios的一些心得技巧分享

    關(guān)于使用axios的一些心得技巧分享

    vue更新到2.0之后,作者就宣告不再對(duì)vue-resource更新,而是推薦的axios,所以下面這篇文章主要給大家分享了關(guān)于使用axios的一些心得技巧,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-07-07
  • 詳解Angular的8個(gè)主要構(gòu)造塊

    詳解Angular的8個(gè)主要構(gòu)造塊

    Angular 主要分為八大構(gòu)造塊(也就是八個(gè)核心概念):模塊、組件、模板、元數(shù)據(jù)、數(shù)據(jù)綁定、指令、服務(wù)、依賴注入。有興趣的可以了解一下
    2017-06-06
  • Angular中ng-bind和ng-model的區(qū)別實(shí)例詳解

    Angular中ng-bind和ng-model的區(qū)別實(shí)例詳解

    這篇文章主要介紹了Angular中ng-bind和ng-model的區(qū)別實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼

    angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼

    本篇文章主要介紹了angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • AngularJS 實(shí)現(xiàn)彈性盒子布局的方法

    AngularJS 實(shí)現(xiàn)彈性盒子布局的方法

    本文給大家?guī)?lái)一段簡(jiǎn)短代碼實(shí)現(xiàn)angularjs彈性布局效果,非常實(shí)用,對(duì)angularjs彈出布局知識(shí)感興趣的朋友可以參考下
    2016-08-08
  • angular forEach方法遍歷源碼解讀

    angular forEach方法遍歷源碼解讀

    這篇文章主要為大家詳細(xì)了angular forEach方法遍歷源碼,forEach()方法用于遍歷對(duì)象或數(shù)組,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 詳解利用Angular實(shí)現(xiàn)多團(tuán)隊(duì)模塊化SPA開發(fā)框架

    詳解利用Angular實(shí)現(xiàn)多團(tuán)隊(duì)模塊化SPA開發(fā)框架

    本篇文章主要介紹了詳解利用Angular實(shí)現(xiàn)多團(tuán)隊(duì)模塊化SPA開發(fā)框架,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • Angular使用ng-messages與PHP進(jìn)行表單數(shù)據(jù)驗(yàn)證

    Angular使用ng-messages與PHP進(jìn)行表單數(shù)據(jù)驗(yàn)證

    這篇文章主要介紹了Angular使用ng-messages與PHP進(jìn)行表單數(shù)據(jù)驗(yàn)證,ng-messages提供了更方便的表單數(shù)據(jù)驗(yàn)證服務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12

最新評(píng)論