Angular Material Design for Input Validation

Having trouble finding an answer that fits my situation...

Currently, I am using angular material design to create a sign in/up form. When no text is entered into the field, the label and bottom border turn red due to the use of ng-required="true".

I want the label and bottom border to turn green when the information is valid, but I'm not sure if there is another directive in Angular that could achieve this. Any suggestions?

Alternatively, I have some CSS in one of my files:

.ng-invalid.ng-dirty {
    border-color: #FA787E; (red)
}
.ng-valid.ng-dirty {
    border-color: #78FA89; (green)
}

However, attaching the class does not seem to be working for me. Here's an example of code for one line of input:

<md-input-container>
    <label class="userlabel">Username</label>
    <input ng-model="credentials.username" type="text" ng-required="true" aria-label="password" class="inputtext ng-valid">
</md-input-container>

If anyone has any insights or solutions, I would greatly appreciate it. Thanks in advance!

Answer №1

Your CSS is headed in the right direction, but you just need to include the .md-input class like so:

.md-input.ng-invalid.ng-dirty {
    border-color: #FA787E;
}
.md-input.ng-valid.ng-dirty {
    border-color: #78FA89;
}

Check out this JSFiddle example
Alternatively, you can also add !important to your current css border-color declaration for added emphasis, like this:

border-color: #FA787E !important;

Answer №2

 <md-input-container>

   <label class='userlabel'>Username</label>

   <input ng-model='credentials.username' type='text' aria-label="password" class="inputtext" required>

 </md-input-container>

I omitted the ng-required attribute and added the required attribute directly to the input tag. This change will result in the input tag being marked as ng-invalid by default. Please confirm if this alteration resolves the issue.

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

Having trouble properly implementing variable updates when creating a multi-theme website

I have a Next.js app and a globals file containing all the themes: body { margin: 0; font-family: Inconsolata, monospace; background-color: var(--bg-color); } :root { --bg-color: #262a33; --main-color: #43ffaf; --sub-color: #526777; --sub-al ...

Is it possible that following or unfollowing a user through Ajax can result in the href attribute of other links changing? Check out the

Attempting to clarify a peculiar situation with as much detail as possible. Attached are images/examples. A specific page is dedicated to displaying all the users a particular user follows. users_controller.rb def following @pals = current_user.followi ...

Tips for retrieving a value from an async function called within the .map function in React?

After doing some research, I discovered that async functions return a promise whose result value can be accessed using .then() after the function. This is the reason why it's not rendering properly. My question is: how can I render the actual value fr ...

In the world of coding, passing an array by reference may seem

Can you explain why console.log(a) and console.log(b) do not return the same result of [1, 2, 3, 4]? function test(c, d) { c = [1, 2, 3, 4]; d.push(4); } a = [1, 2, 3]; b = [1, 2, 3]; test(a, b); console.log(a); console.log(b); ...

URL base not refreshing on globalized Angular web application

I have recently expanded my Angular web app to include new languages, but I am encountering a strange issue. Despite setting up all the new languages in my angular.json file and linking their respective language files correctly, I am facing a problem with ...

How can I convert a string number with leading zeros into a string in a node.js environment?

When rendering a page, I pass the id as a string (e.g. 001, 002) but at the client side, I receive it as a number (e.g. 1, 2). res.render('leafletDemo',{id:userID,latitude:latitude,longitude:longitude}); Is there a way to keep the id as a str ...

Navigating an Angular JSON object: A guide

I have successfully created a nodejs application that reads all the databases in my mongo Db when I run it in the console. However, I am facing an issue when trying to parse the data into a json object and display it on the screen. If anyone can guide me o ...

Resolving Typescript jQuery AJAX Navigation Problem

Hello dear Earthlings, The code snippet below is currently implemented on my website brianjenkins94.me for handling basic navigation functionalities. After some recent changes, I encountered an issue with the #nav.on("click") event handler not working as ...

Add a fresh widget to the DOJO attachment point without overwriting

Here is some code I have: postCreate: function() { this.items.forEach(lang.hitch(this, function(item) { new SideNavigationItem({ name: item.name }, this.container); })); } This code snippet ...

Dilemma: Overlapping Dropdowns on a Bootstrap Navbar Floated right

When using Bootstrap 3.2 and applying navbar-right to two different dropdown menus, they tend to overlap slightly. The code snippet below is taken directly from the Dropdown examples on the official Bootstrap website, but with the modification of having tw ...

Error connecting to the network: apisauce

Encountered a NETWORK_ERROR while attempting to fetch data from my Django server. CORS headers are functioning correctly on the backend. I have tried using localhost and my machine's IP address, but it still does not work. Here is the result of con ...

Filter an array containing nested objects based on dynamically determined properties

I'm working with an array of N objects and need to create a filter using JSON.stringify that dynamically checks multiple properties. Looking for a solution that is dynamic and doesn't rely on static properties (as shown in the code snippet above ...

Guide on passing the set[State] function to a trigger component that is not a descendent

Take a look at this diagram. ChildComponentB contains a state called stateX. In ChildComponentA, when a certain event occurs, it should modify the stateX in ChildComponentB. If ChildComponentA is a child component of ChildComponentB, passing the setStateX ...

ng-repeat to prevent duplicate items from displaying

I intentionally have duplicates in my array of items and I am looking for a way to hide just one of them when clicked within my ng-repeat. Is there a way to hide only one of the duplicated items on click? I feel like I might be overlooking something, as ...

Make sure to trigger a callback function once the radio button or checkbox is selected using jQuery, JavaScript, or Angular

I'm looking to receive a callback once the click event has finished in the original function. <input type="radio" onchange="changefun()" /> function changefun() { // some code will be done here } on another page $(document).on('input: ...

The appearance of responsive CSS is altered when deployed compared to when it is viewed on the

I'm a beginner in web development and facing an issue with my mobile CSS. The strange thing is that when I view it on localhost, everything looks great, but once deployed (tried both Heroku and GitHub), it appears distorted. Even when I make extreme c ...

JavaScript is unresponsive and fails to display

I attempted to incorporate my initial Javascript snippet into a browser to observe its functionality. However, upon adding these lines directly into the body of my HTML code (even though I am aware that there are more efficient methods), no visible changes ...

Tips for directing focus to a specific div or success message following a form submission

After numerous attempts, I am still struggling to focus on the success message following form submission. The backend PHP code is already in place to upload data into the database and display the success message, but every time I submit the form, the page ...

The styles of Bootstrap 4 are not being used

My Bootstrap 4 css file isn't working properly and I need space for my navigation bar. <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-bottom"> <div class="container"> <a class="navbar-brand" href="#">Start Bootstrap ...

Issue encountered when attempting to assign an action() to each individual component

I'm facing an issue with the button component I've created. import { Component, OnInit, Input } from '@angular/core'; @Component({ selector: 'app-button', template: ` <ion-button color="{{color}}" (click)="action()"&g ...