Angular 4 marks an email input as ng-valid even when it is invalid

Is there a known issue with Angular 4 template driven validation failing, or is it possibly a bug in my code?

Here is the HTML snippet:

<div class="form-group">
  <label for="email">Email</label>
  <input type="email" class="form-control" id="email" required 
    [(ngModel)]="model.email" name="email" #email="ngModel" #spy>

  <div>TODO remove: {{spy.className}}</div>
  <div [hidden]="email.valid || email.pristine" class="alert alert-danger">
    Valid email is required
  </div>
</div>

The component class includes the property: model = new SignupModel('','','','');

Related CSS styles include:

input:invalid:not(.ng-pristine) {
    border-left: 5px solid #a94442; /* red */
}
input:valid:not(.ng-pristine) {
    border-left: 5px solid #42A948; /* green */
}

Upon initial page load, the email input has classes: form-control ng-untouched ng-pristine ng-invalid The alert message remains hidden because email.pristine === true

If a user starts typing "aa", the class names change to: form-control ng-dirty ng-valid ng-touched Now, the error message is no longer visible as email.valid === true, but the left border shows up as red due to invalid input.

Why does Angular mark email.valid as true and applies the ng-valid class, when the CSS correctly identifies the input as invalid?

The ng-invalid class is applied when the invalid input "aa" is removed, indicating that the "required" field is working correctly.

Answer №1

One way to enhance this form is by including a new "email" attribute within the input tag:

<input type="email" class="form-control" id="email" required email 
    [(ngModel)]="model.email" name="email" #email="ngModel">

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

What are the reasons for avoiding the use of fstat()?

As I delve into the documentation for the stat method here, an important piece of advice caught my attention: The recommendation is not to use fs.stat() to check if a file exists before performing fs.open(), fs.readFile(), or fs.writeFile(). Instead, it s ...

Eliminate full stops from within the XML elements

I encountered a strange issue where the XML files contain periods (.) within them. <member> <name>ABCD</name> <value> <date.of.birth>02.05.2000</date.of.birth> </value> ...

Exploring the Integration of jQuery AJAX in a Contact Form

I would like to incorporate AJAX functionality into a contact form. Here is the current code I have... $("#contact_form").validate({ meta: "validate", submitHandler: function (form) { $('#contact_form').hide(); ...

Issue with Route Transition in React Router version 4 when using Styled Components

I am currently in the process of constructing a React Application utilizing React-Router@4, <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5e7d0d4d6c198fddac198f9dad4d1d0c7f5869bd7d0c1d49b83">[email protected]</a> ...

Implementing mock JSON Data in Angular

I'm currently exploring Angular and working on a project that involves retrieving data from an API and displaying it in an HTML table. Right now, I am experimenting with manipulating the table data to update the model (JSON). While I understand that m ...

Exploring Typescript for Efficient Data Fetching

My main objective is to develop an application that can retrieve relevant data from a mySQL database, parse it properly, and display it on the page. To achieve this, I am leveraging Typescript and React. Here is a breakdown of the issue with the code: I h ...

Achieve a "upload file" button

I am trying to create an add file button within a sidebar of a standard editor. Here are the steps involved: There is a + button. When clicked, it reveals an input field. The user enters the name of the new file and presses enter on the keyboard to s ...

Aframe audio gap control

I am currently working on a scene that includes a looping sound: <a-assets> <audio id="ambience" src="./audio/ambient.mp3" preload="auto"></audio> <a-entity id="ambience_sfx" sound="src: #am ...

Issues with the code: $("input[type=checkbox]:checked").each(function() { var checkboxId = $(this).attr("id"); });

When submitting a form, I need to retrieve the IDs of all checked checkboxes. Currently, $(this).id() is causing an error. What is the correct code to obtain the IDs of all checked checkboxes? $("#pmhxform input:checkbox:checked").each(function() { ...

Using an iframe in an Angular 2 app can cause the ngOnInit function of the App Component

As I develop an Angular 2 application that will be embedded in iframes on various websites, I encountered a peculiar issue during testing. It appears that the App Component's ngOnInit() function gets triggered twice upon loading the application within ...

Error encountered while attempting to validate and add new entries

I am facing a challenge while attempting to insert a record into my mongodb database. Despite providing what seems to be the correct values, mongoose is flagging them as missing. Below is the Schema I am working with - var mongoose = require('mongoo ...

What is the best method to insert multiple rows of the same height into a table cell in

My datatable is causing me trouble as I try to add more rows in the td after the Name column. The rows are not aligning properly, and I need a solution. I want these new rows to be aligned with each other, but the number of rows may vary based on user inp ...

Tips for creating AngularJS forms which display radio buttons and populate data from a JSON file

I'm having trouble displaying the json data correctly. How can I show the radio buttons from my json file (plunker demo)? Additionally, I want to validate the form when it is submitted. Here is the HTML code: <my-form ng-app="CreateApp" ng- ...

When the onChange event is triggered, the current value in my text input field disappears in React

Just starting out with Reactjs and I'm working on creating a page where users can edit their own posts. Currently, when I try to modify the existing text in the text input, the form gets cleared and the user has to re-enter the data. This is not the b ...

Ways to access real HTML components within a Chrome extension instead of the initial source code

I am currently developing a Chrome extension, When using Chrome's Inspect Elements panel, all HTML page elements are displayed at the bottom. Is there a way to retrieve all those elements using JavaScript? The original source code of the page does ...

The v-select menu in Vuetify conceals the text-field input

How can I prevent the menu from covering the input box in Vuetify version 2.3.18? I came across a potential solution here, but it didn't work for me: https://codepen.io/jrast/pen/NwMaZE?editors=1010 I also found an issue on the Vuetify github page t ...

Error Alert: jQuery AJAX Event is not defined in Firefox Browser

This snippet works perfectly on Webkit browsers, but for some reason, it's not functioning in Firefox. I have a feeling that I need to include "event" within parentheses somewhere, but I'm unsure about the correct placement. Any ideas? html < ...

Troubleshooting issues with $scope in an Angular Schema Form modal

Plunker This Plunker demo allows for editing rows in a grid. A new method has been implemented based on RowEditCtrl to insert a new row, but there are issues with validation. Upon inserting a new row, the form initially appears as "pristine and valid". T ...

Forward to a SubDomain

Utilizing Yellowtree's GEOIP-Detect plugin, I attempted to implement a location-based redirection system for visitors. Unfortunately, I encountered issues with the code execution. The process initially involves extracting the user's IP address an ...

What could be the reason my Bootstrap 5 dropdown menu is not functioning correctly?

As of late, I've delved into web development and have been utilizing Bootstrap v5.0 for creating a website. The code below is from a file named "grage.php": <!-- HERE SHOULD BE THE CODE OF GARAGE --> <div class="container-fluid" ...