GitHunt
AN

AndrewJBateman/angular-form-validation

:clipboard: This app sets up form validation in Angular 10 using Template-Driven Forms. This is a simple registration form with standard fields for first name, last name, email, password and confirm password. Additional field with maxLength validator added.

โšก Angular Form Validation

  • This app sets up form validation in Angular 10 using Template-Driven Forms.
  • This is a simple registration form with standard fields for first name, last name, email, password and confirm password. Additional field with maxLength validator added.
  • Code from Blog by Jason Watmore: - see ๐Ÿ‘ Inspiration below.
  • Not to be upgraded from Angular 10.

*** Note: to open web links in a new window use: ctrl+click on link**

๐Ÿ“„ Table of contents

๐Ÿ“š General info

  • Input fields of main form have validation so incorrect inputs trigger a red boundary around the input field and an error message.
  • Styling of the template-driven forms is done using Bootstrap.

๐Ÿ“ท Screenshots

Example screenshot.

๐Ÿ“ถ Technologies

๐Ÿ’พ Setup

  • Install dependencies using npm i.
  • Run ng serve for a dev server.
  • Navigate to http://localhost:4200/. The app will automatically reload if you change any of the source files.

๐Ÿ’ป Code Examples

  • div with firstName field entry that is validated using a validator and directive called MustMatch
<div class="form-group">
  <label for="firstName">First Name</label>
  <input
    class="form-control"
    name="firstName"
    [(ngModel)]="model.firstName"
    #firstName="ngModel"
    [ngClass]="{ 'is-invalid': f.submitted && firstName.invalid }"
    required
  />
  <div *ngIf="f.submitted && firstName.invalid" class="invalid-feedback ">
    <div *ngIf="firstName?.errors.required">
      First Name is required
    </div>
  </div>
</div>

๐Ÿ†’ Features

  • All fields are required, the email field must be a valid email address and the password field must have a min length of 6.
  • A custom validator and directive called MustMatch is used to validate that the confirm password and password fields match.
  • The form validates on submit rather than as soon as each field is changed, this is implemented using the f.submitted property of the #f="ngForm" template variable which is true after the form is submitted for the first time.

๐Ÿ“‹ Status & To-Do List

  • Status: Working.
  • To-Do: Nothing.

๐Ÿ‘ Inspiration

๐Ÿ“ License

  • This project is licensed under the terms of the MIT license.

โœ‰๏ธ Contact