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
๐ถ Technologies
- Angular v10
- Bootstrap v4 component library used.
๐พ Setup
- Install dependencies using
npm i. - Run
ng servefor 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
- Repo created by ABateman, email: gomezbateman@yahoo.com
On this page
Languages
TypeScript59.9%HTML29.9%JavaScript9.8%CSS0.5%
Contributors
Created November 22, 2018
Updated November 10, 2025
