Skip to content
tezvyn:

Angular's Built-in Form Validators

Source: angular.devEasyHow cards are made

Angular's Built-in Form Validators

Angular's built-in validators are pre-made rules for your forms. Use them to easily check for required fields, min/max length, and valid email formats. The footgun is confusing required for text inputs with requiredTrue, which is only for checkboxes.

Why it exists

User input needs to be validated to ensure data integrity before it's processed or sent to a server. Writing logic for every common case—like checking if a field is empty, has a minimum length, or is a valid email—is repetitive and error-prone. Angular provides a standard library to handle these common cases out of the box.

The mental model

Think of the Validators class as a toolbox of pre-built functions you attach to your form fields. Instead of building your own "is this an email?" checker from scratch, you just grab Validators.email from the toolbox. These are static methods that you import and apply directly to a FormControl.

How it works

The Validators class provides static methods like Validators.required, Validators.minLength(5), or Validators.pattern(/\d+/). You pass these functions (or an array of them) as the second argument when creating a FormControl. When the control's value changes, Angular runs each validator function. If validation fails, the function returns an error object (e.g., { required: true }); otherwise, it returns null. The form control's errors property will then hold this object, which you can use to display contextual error messages in your template.

When to use it

Use built-in validators for any standard, single-field validation need in both template-driven and reactive forms. They cover the most frequent use cases: checking for presence (required), ensuring a checkbox is ticked (requiredTrue), enforcing length (minLength, maxLength), setting numerical bounds (min, max), and matching formats (email, pattern).

When not to use it

Built-in validators are not suited for complex, multi-field validation (e.g., "password must not contain username") or for asynchronous checks (like verifying a username's availability with a server). For those scenarios, you need to write a custom validator or an async validator, respectively.

One canonical example

To create a form control for a password that must be at least 8 characters long, you would write: const password = new FormControl('', Validators.minLength(8));. If a user enters '12345', the control becomes invalid. Checking password.errors would return an object like { minlength: { requiredLength: 8, actualLength: 5 } }, which you can use to show a specific error message.

Interview question

Which validation scenario would typically require a custom validator rather than a built-in Angular validator?

  • a.Validating that a password input matches its 'Confirm Password' counterpart.Correct
  • b.Checking if a text field contains a valid email address format.
  • c.Confirming a user has entered at least 5 characters in a username field.
  • d.Ensuring a 'Terms and Conditions' checkbox is checked.
Why?

The card explicitly states that built-in validators are not suited for complex, multi-field validation like comparing two password fields, which would require a custom validator. The other options are all handled by specific built-in validators (minLength, requiredTrue, and email, respectively).

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles