"Reactive Forms in Angular: Troubleshooting the updateOn Function

Is there a way to display the validation message as I type, rather than only when the input loses focus? I've tried the following:


form: FormGroup = new FormFroup({
    direction: new FormControl("", {
    validators:[Validation.required,Validation.pattern("^[0-9]*$")],
    updateOn: 'change'
}

In the HTML, I haven't subscribed to the (change) event - could this be the issue? If so, what do I need to do in order to get the validation to happen on typing changes?

Answer №1

Typically, the validator will execute when there is a change in the control value.

  form: FormGroup = new FormGroup({
    direction: new FormControl(null, {
       validators: [Validators.required, Validators.pattern("^[0-9]*$")]
    })
  });

If you need to display an error message based on validation errors, you can check if there are errors and use the hasError method to target specific errors.

template

<div [formGroup]="form">
  <input formControlName="direction">
  <div *ngIf="form.get('direction').errors">
      <div *ngIf="form.get('direction').hasError('required')">This field is required</div>
      <div *ngIf="form.get('direction').hasError('pattern')">Invalid pattern 🚫🚫 </div>
  </div>

</div>

Feel free to check out this demo 👉👉 demo 🚀

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Problem with organizing data by dates

My timers list looks like this: timer 1 => { startDate = 17/01/2019 11PM, endDate = 18/01/2019 9AM } timer 2 => { startDate = 18/01/2019 7AM, endDate = 18/01/2019 1PM } timer 3 => { startDate = 18/01/2019 12PM, endDate = 18/01/2019 10PM } time ...

How to Embed HTML Tags in a TypeScript File in Angular

I'm currently utilizing Angular2 and I'm looking to incorporate an HTML tag inside the return function in a TypeScript file. tooltip: (param: any) => { return `<span> ${param.value} </span>`; } I've attempted ...

I need help figuring out how to convert a date into Spanish using an Angular pipe in an Ionic 3 application

I need to display a date in Spanish in my Ionic project that I am fetching from SQL Server 2014. Currently, the date is being formatted using an Angular pipe, but it is showing up in English. I have attempted to use I18n for internationalization, but it do ...

Angular app encountering issues after trying to add new package

After making a clone of an existing Angular project, I successfully ran the application using ng serve. Upon wanting to add the SignalR package, I used the command: npm install @aspnet/signalr –-save The installation seemed to go smoothly at first. Howe ...

Ways to update the value of an object in typescript

When working with an array of objects, I encountered an issue while trying to change the object value. The error message "Type 'string | boolean' is not assignable to type 'never'. Type 'string' is not assignable to type &apos ...

Angular 8 allows for the utilization of Ul li elements to create an expandable and collapsible hierarchical

As a newcomer to Angular, I have managed to code a representation of hierarchical JSON data in a tree view using recursive calls. The code works well, but I am faced with the challenge of implementing an expand and collapse functionality for the treeView u ...

Issue encountered with executing `ng build` in Angular Bitbucket Pipeline

I have set up a pipeline in Bitbucket to deploy my Angular app to an FTP server. The pipeline.yml file for this setup looks like this: image: node:6.9.4 # we need node image to run our angular application in clone: # help to clone our source here de ...

Exploring a three.js object using a smartphone camera movement

I'm working on creating an AR effect by placing a simple rectangle on my camera preview. The goal is to have the rectangle stay fixed in position even as the camera moves. I understand that I need to access the gyroscope of my smartphone to achieve th ...

Persistent CORS issue persists in MEAN Stack despite implementing CORS middleware

Currently, I am building a web application using Angular with NodeJs as the backend. Despite implementing CORS middleware, I am encountering CORS errors. Please take a look at the code snippet below: var mysql = require('mysql'); const fs = requi ...

The method getDay is not recognized as a function in Typescript Angular

After searching for a solution, I came across a similar question that did not address the specific issue I am dealing with. The challenge I am facing involves parsing a date into a string. Below is my current implementation: export class DataFormater { ...

Determining in Angular 8 whether a value has been altered by a user or by a method call

Within my select element, the value is currently being assigned through an ngOnInit call. Here is an example of the HTML code: <select name="duration" [(ngModel)]="exercisePlan.duration" (ngModelChange)="onChange($event)"> <option *ngFor="l ...

Stop Ag grid from automatically extending to the entire width of the screen

I am encountering an issue where my table scales to the full width available even when the content is limited. Take a look at the demo here: "https://plnkr.co/edit/6L8bTAwkEV6R6Be4M1Qm?p=preview" I have attempted to address this problem by settin ...

Struggling with Navigation Guard integration in a Vue 3 + Quasar + TypeScript application

Currently, I am developing an application using the Quasar Framework in conjunction with Vue 3 and TypeScript, specifically utilizing the Composition API. My objective is to incorporate a Navigation Guard within my routes.ts file for handling route authent ...

Guide on dividing a URL string in Angular framework

Is there a way to include a value directly in the URL, like so: http://example.com/component/july2021 I need to extract july2021 from the component and separate it into "july" and "2021". How can I achieve this? ...

What is the process for disabling other group options in md-select when selecting an option from one group?

I'm currently working on creating a dropdown menu using the <md-select> tag alongside <md-optgroup>. I've been referencing the "Option Groups" example found here: https://material.angularjs.org/latest/demo/select However, I want ...

Ways to confirm if the indexed sort is an extension of a string

Suppose I have a function called func with 2 generic arguments const func = <T extends {}, K extends keyof T>() => {}; and a type defined as interface Form { a: boolean; b: string; } then I can call them without encountering any errors func& ...

Error: Unable to build Angular app in Docker container due to missing file or directory

I am facing an issue with my GitLab CI pipeline. Here is the configuration: image: docker:stable variables: GIT_STRATEGY: clone DOCKER_HOST: tcp://localhost:2375 DOCKER_TLS_CERTDIR: "" DOCKER_DRIVER: overlay2 CONTAINER_RELEASE_IMAGE: $ ...

Unable to successfully install Angular CLI through npm

Having trouble installing Angular CLI with the command npm install @angualr/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f5c53567f0e1108110a">[email protected]</a>. I encountered the error shown in the screen ...

Custom Mui table sizes - personalized theme

By implementing custom sizes for the Table component in Material UI, I have extended the Table size prop with the following declaration: declare module '@mui/material' { interface TablePropsSizeOverrides { relaxed: true large: true } ...

Angular 11 is throwing an error stating that the type 'Observable<Object>' is lacking certain properties as required by its type definition

My code is producing the following error: TS2739 (TS) Type 'Observable<Object>' is missing the following properties from type 'WeatherForecast': ID, date, temperatureC, temperatureF, summary I'm puzzled as to why this error ...