Non-functional CSS selector not functioning properly in reactive form

I am encountering an issue where I am unable to bind the :invalid CSS selector with my input due to having custom validation. The invalid state only activates with a basic validator example like input type="email", so it will work with examples such as example@ or [email protected] while my function only validates the latter ([email protected]). Project Link

HTML FILE

<input class="custom-input" type="email" [(ngModel)]='Email' name="email" placeholder="Email" formControlName="emailTxt">
<div class="err>
   this is an error
</div>

TypeScript File

this.signUpForm = this._fb.group({
   emailTxt: [null, Validators.compose([
     Validators.required
   ])]
 }, {
     validators: [
       EmailValidation('emailTxt'),
     ]
   });

Form Validation Function

export function EmailValidation(controlName: string) {
  return (formGroup: FormGroup) => {
    const control = formGroup.controls[controlName];

    if (control.errors && !control.errors.invalidEmail) {
      return;
    }
    if (control.value.trim().match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{1,5}|[0-9]{1,3})(\]?)$/) == null) {
      control.setErrors({ invalidEmail: true });
    }
    else {
      control.setErrors(null);
    }
  }
}

CSS File

.custom-input:invalid{
    & ~ .err {
      max-height:200px;
      padding:0 18px 10px 20px;
      color:red;
    }
}

I expect the invalid selector to be triggered with every control.setErrors() function.

Answer №1

To implement the following CSS code in your project, it is guaranteed to be effective:

input.ng-dirty.ng-invalid {
  border: 1px solid red;
}

Answer №2

Make sure to include this in your CSS stylesheet for guaranteed success! 😊

input.ng-invalid.ng-touched, select.ng-invalid.ng-touched {
    border: 1px solid red !important;
}

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

Expanding HTML Editor's Functionality in Eclipse with Custom Tags

Creating an application involves incorporating custom tags within HTML code, which are processed on the server side before being presented to the end user as valid HTML. An example of using custom tags is shown below: <html> <body> ... < ...

Guide to capturing and playing audio on iOS6 with HTML5

Our team is currently working on a web application using HTML5 and Javascript. We are facing a challenge with implementing voice recording functionality, as the Wami Api we tried is not compatible with iPad due to its use of flash for recording. Could yo ...

Is there a way to select multiple items using the same xpath in Python Selenium by clicking on them?

I am facing a challenge with my webpage code. The code snippet below presents a radio button setup: <label data-correct="false"> <input type="radio" ng-model="quizCtrl.state.selectedOptionForCurrentQuestion&quo ...

Loading data from a remote source in an Angular 6 material table: A step-by-step guide

I'm currently puzzled by the usage of the material table schematic and am struggling to find a solution. It appears that the key aspect lies within the connect() method present in the datasource.ts file. Simply put, this.data is merely an array. / ...

I am looking to test my Angular 2 application using Team City - what testing solutions are available to me?

In the upcoming two-year project, we are embarking on the development of a robust Angular 2 application. As part of our testing strategy, we will be implementing User Interface Tests. While our Unit Tests and Integration Tests will be written in C# using ...

Using MaterialUI to create a GridListTile with two IconButtons

I'm working with a GridListTile and trying to add a second button, but I'm having trouble getting both buttons to display. Even though I've attempted to include two ActionIcons, only one of them is showing up. Here's the code snippet: ...

using jQuery to show a block element

I'm attempting to determine whether a div with the style display as block then perform an action. Here is an example: This is just an idea I'm trying to implement using jQuery. if ($("#toshow").css("display") == "block"){ }else{ } ...

Trouble displaying CSS content image in Internet Explorer

Usually, I use Chrome as my default browser. However, I recently decided to test whether my website functions properly on other browsers as well. In Chrome, the header displays my logo with great quality using content:url. But when I tried accessing my w ...

What is the method for setting a fixed size for a Bootstrap container?

Bootstrap 4 is the framework I am currently using. Below is the HTML code I have written: <div class="sprite container d-inline-block bg-info rounded p-1"> <span class="sprite-span-level">Top-right text over image</span> <img cl ...

Tips for utilizing dynamic variables when setting props in React Native styles

I am encountering an issue while trying to set props in the stylesheet to dynamically change the background color based on data received from the server. undefined is not an object (evaluating 'this.props.colorCode') Below is the snippet of my ...

Navigate through the options on the left menu by sliding your

I'm attempting to create a menu that slides in a submenu from the left on hover, but the issue is that with this code, all submenus open instead of just the one related to the hovered link. Here is my HTML code: <ul id="NavMenu"> ...

IE having issues with selecting all options button in jQuery, but it is functioning correctly in Firefox

In one part of my cgi code, I create the following: my $PARAMETER_HTML .= "<select name='parameters' id='parameters' size='10' multiple='multiple'>"; foreach my $values (sort @PARA_VALUES) { $PARAMETER_HTM ...

What methods are most effective when utilizing imports to bring in components?

Efficiency in Component Imports --Today, let's delve into the topic of efficiency when importing components. Let's compare 2 methods of importing components: Method 1: import { Accordion, Button, Modal } from 'react-bootstrap'; Meth ...

What is the correct way to apply background-position percentage in this situation?

This problem has got me at my wit's end. Imagine a series of divs lined up horizontally. Each one is sized as a percentage of the total width, but that total width is unknown. For instance: --------------------------------- | | | ...

Is HTML-escaping necessary when implementing regex checks?

As I develop a web application that takes user input through HTML inputs and forwards it to my tomcat server for processing, my current workflow is as follows: Client JS -> collect HTML input -> perform regex validation -> if successful -> se ...

What is causing the browser to completely ignore the viewport meta tag and mobile style sheet implementation?

I am currently working on setting up a website where I want the page to initially appear zoomed out all the way using a viewport meta tag. Additionally, I would like to use a separate stylesheet for mobile devices. However, no matter what I try, the page d ...

What could be causing a fixed pseudo-element to interfere with clicks on my website?

After updating my website to Bootstrap5, I noticed that one of the pages was not behaving correctly. Upon investigation, I found that the issue was related to this particular block of CSS: body:before { content: ''; position: fixed; w ...

The justify-content property is failing to properly align content along the main axis in Grid layout

I've implemented tailwind for my CSS styling. Within a simple div, I have 3 child elements. My goal is to create space between them along the main axis. Here's the code snippet in question: <div className="grid grid-cols-12 text-white ju ...

Exploring the capabilities of automation testing with charts.js and the latest version of Angular

While working on my testing automation for charts.js, I utilized the ngContext object to retrieve data with this code snippet: document.getElementsByTagName('chart-dataset')[0].__ngContext__. However, since upgrading to angular 14, it seems that ...

Stuck with the same icon even after a successful AJAX call

I am currently working on implementing a 'add to my list' function in my web app. The goal is to change the color of an icon when a user clicks on it, after sending the necessary data to the server. Despite successfully sending the data to the s ...