Reactive Forms Email And Password Pattern Validations In Angular

Step 1

To create a Reactive from, you need to import the ReactiveFormsModule from @angular/froms in app.module.ts page.
  1. import { BrowserModule } from'@angular/platform-browser';  
  2. import { FormsModule, ReactiveFormsModule } from'@angular/forms';  
  3. @NgModule({  
  4.    BrowserModule,  
  5.    BrowserModule,  
  6.    ReactiveFormsModule,  
  7. }),  
  8. exportclassAppModule { }  
Step 2

Next create user module page for login form. Create component and design pages, look for component.ts and component.html pages within our app folder.
  1. import { Component, OnInit,TemplateRef,ViewChild, ElementRef } from'@angular/core';  
  2. import { FormBuilder, FormGroup, Validators, FormControl, AbstractControl } from'@angular/forms';  
Here I have added some Angular libraries.

Step 3

Add form selector id and template url path. We can access it from control design component.html page.

Form just contains email and password validations in Angular. We can access the pattern for email and password for the below pattern. Password just contains one alphabet letter, one number, and one special character.
  1. @Component({  
  2.    selector:'module-id',  
  3.    templateUrl:'./login.component.html',  
  4. })  
  5. exportclassLoginComponentimplementsOnInit {  
  6.    loginForm: FormGroup;  
  7.    constructor(privatehttp: Http,privatefrombuilders: FormBuilder){  
  8. }  
  9. ngOnInit() {  
  10. this.Form = this.frombuilders.group({  
  11.    Email: ['', [Validators.required, Validators.pattern('^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$')]],  
  12.    Password: ['', [Validators.required, Validators.pattern('(?=.*[A-Za-z])(?=.*[0-9])(?=.*[$@$!#^~%*?&,.<>"\'\\;:\{\\\}\\\[\\\]\\\|\\\+\\\-\\\=\\\_\\\)\\\(\\\)\\\`\\\/\\\\\\]])[A-Za-z0-9\d$@].{7,}')]],  
  13. });  
  14. }  
Content was composed with the instant online HTML editor. Please purchase an HTMLg license to remove the promotional messages from the edited documents.