AI Hints
Home›Angular Tutorial›Signal Forms, Custom Validators and Practice: A Signup Form
Angular · Lesson 90 of 120

Practice: A Signup Form

Your turn.

⏱ 2 min read▶ Video at 1:47:57🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Let's practise forms. You'll build a signup form, with a name, a password, and a password check.

  • A signup form
  • Name and password
  • Your turn

A signup form

Your turn. Pause the video, and try this. Build a reactive signup form. The name is required, and the password needs eight characters. A group validator checks that the two passwords match. On submit, welcome the user. Here is one solution. The form group has three controls. The passwords match validator goes on the group. We filled in matching passwords, and signed up as Sam.

✍️ Exercise: A signup form

Reactive form: name (required), password (minLength 8), confirm. Group validator passwordsMatch. Disable "Sign up" while invalid; on submit show "Welcome, NAME!".

Show hint

new FormGroup({ ... }, { validators: passwordsMatch })

Show solution
app.tstypescript
import { Component, signal } from '@angular/core';
import { ReactiveFormsModule, FormGroup, FormControl, Validators } from '@angular/forms';
import { passwordsMatch } from './validators';

@Component({
  selector: 'app-root',
  imports: [ReactiveFormsModule],
  templateUrl: './app.html',
})
export class App {
  done = signal('');
  signup = new FormGroup({
    name: new FormControl('', Validators.required),
    password: new FormControl('', Validators.minLength(8)),
    confirm: new FormControl(''),
  }, { validators: passwordsMatch });
}
Output
[DOM] Input elements should have autocomplete attributes (suggested: "new-password"):
(More info: https://goo.gl/9p2vKq) %o
[DOM] Input elements should have autocomplete attributes (suggested: "new-password"):
(More info: https://goo.gl/9p2vKq) %o

app.html

The template uses template URL, so it lives in app dot html. The mismatch message shows after the user types in the confirm field. And the button stays disabled, until the whole form is valid.

app.htmlhtml
<form [formGroup]="signup" (ngSubmit)="done.set(signup.value.name ?? '')">
  <input formControlName="name" placeholder="Name">
  <input formControlName="password" type="password" placeholder="Password">
  <input formControlName="confirm" type="password" placeholder="Confirm password">
  @if (signup.hasError('mismatch') && signup.controls.confirm.dirty) {
    <p class="error">Passwords don't match.</p>
  }
  <button [disabled]="signup.invalid">Sign up</button>
</form>
@if (done()) { <p>Welcome, {{ done() }}!</p> }

What module ten taught

Ng model binds inputs in template driven forms. Form controls, and groups, build reactive forms in the class. Signal forms build a form from a signal. And validators, built-in or your own, check every field.

keyvalue
[(ngModel)]template-driven
FormGroup, FormControlreactive
form(signal)signal forms
Validatorsrules, built-in or custom

Summary

You can now build forms, and check what users type. That's the end of module ten. Next, talking to a server, with HTTP.

Key takeaways
  • Forms
  • Validators
  • Errors
Next up: Talking to a Server — HTTP