Display validation messages when the user either clicks out of the textbox or finishes typing

Here are some validation messages:

<form name="dnaform" novalidate>
    <div style="padding-bottom:5px" ng-show="dnaform.uEmail.$pristine || dnaform.uEmail.$valid">Active directory account </div>
    <div style="padding-bottom:5px;" ng-show="dnaform.uEmail.$dirty && dnaform.uEmail.$error.required" class="help-block">Email is Required.</div>
    <div style="padding-bottom:5px;" ng-show="dnaform.uEmail.$error.email" class="help-block">Invalid Email.</div>
    <input type="email" name="uEmail" class="form-control txtBoxEdit type7N" ng-model="useremail" required>
</form>

The issue I'm facing is that the validation message appears instantly when the user starts typing in the email textbox. My goal is to display the message only after the user has entered a full email or moves focus away from the textbox.

Answer №1

Don't forget to create a function that will be triggered when the input loses focus. You can refer to this example

$scope.onBlur = function() {

    };

Make sure to implement this function for the blur event on your input element

<input type="text" ng-blur="onBlur(inputData)" ng-model="inputData">

Answer №2

Utilize the $touched property:

<div style="padding-bottom:5px;" ng-if="dnaform.uEmail.$touched && dnaform.uEmail.$error.email" class="help-block">Invalid Email.</div>

According to the documentation:

  1. ng-touched: indicates that the control has been blurred, meaning once it is touched, validation will occur on subsequent occasions. To avoid this, add it to your <input>:
ng-model-options="{ updateOn: 'blur' }"

Code snippet:

(function() {
  'use strict';
  angular.module('app', [])
    .controller('mainCtrl', function($scope) {

    })
})();
<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
</head>

<body ng-controller="mainCtrl">
  <form name="dnaform" novalidate>
    <div style="padding-bottom:5px" ng-show="dnaform.uEmail.$pristine || dnaform.uEmail.$valid">Active directory account </div>
    <div style="padding-bottom:5px;" ng-show="dnaform.uEmail.$dirty && dnaform.uEmail.$error.required" class="help-block">Email is Required.</div>
    <div style="padding-bottom:5px;" ng-if="dnaform.uEmail.$touched && dnaform.uEmail.$error.email" class="help-block">Invalid Email.</div>
    <input type="email" name="uEmail" ng-model-options="{ updateOn: 'blur' }" class="form-control txtBoxEdit type7N" ng-model="useremail" required>
</form>
</body>

</html>

For further information, refer to this resource.

I trust this clarifies things!

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

Enhancing button images with gradient effects on hover

I'm currently working on adding a color gradient effect to a PNG graphic used as a button. I specifically want the color gradient to appear only when the user hovers over the image, and not just for background images like most solutions seem to provid ...

PHP can be executed and accessed directly from JavaScript without the need for any form submission

While I have come across a lot of information on PHP post/get methods for transmitting data, I find myself stuck with an interesting issue that requires some assistance. My HTML page contains data in different inputs, and I run algorithms on the JavaScrip ...

Determining button click events in VueJS using the Watch method

Creating a feature for undo/redo in VueJS involves watching the settings and adding a new element to an array of changes when there is a setting change. Additionally, there is a method for undoing actions when the corresponding button is clicked. An issue ...

Automatically Refresh a Div Element Every 5 Seconds Using jQuery's setInterval() Function

My goal is to refresh the left div every 5 seconds using $.ajax to get JSON data and display 4 random elements in that div. However, even though the left div block refreshes, the content remains the same with the same images always showing. view image desc ...

What is the best method to trigger a bootstrap modal window from a separate component in Angular 8?

I have successfully implemented a bootstrap modal window that opens on a button click. However, I am now facing difficulty in opening the same modal window from a different component. Below is the code I have tried: <section> <button type=&quo ...

Retrieving the IDs of all socket connections in a Node.js Socket.IO array

I currently have a complex program utilizing socketio 0.9 where I am storing all the sockets id in arrays like so: var clients = {}; To uniquely identify and store my sockets, I assign them a serial and then set the 'socket' key with its actual ...

Utilizing jQuery to dynamically add results and prevent duplicate entries in will_paginate

I am currently using will_paginate to easily manage the comments pagination in my Rails 3 application, and so far it's been working flawlessly. At the moment, I have set up the display to show 10 comments per page. However, whenever I add a new comme ...

CSRF fails to execute during the initial post submission

This is my first time dealing with CSRF and reaching out to Stack Overflow for the first time. After struggling through the configuration, I finally managed to get it working almost perfectly. However, I ran into an issue where if I open a bookmarked page ...

Embedding videos in HTML

I am experiencing difficulties with embedding a video in my HTML file. I attempted to use a YouTube video, but when I open it in my browser, it displays an error message stating that the video is unavailable because YouTube has refused to connect. I also ...

Using Typescript with Angular 2 to Implement Google Sign-In on Websites

Currently, I am in the process of creating a website that utilizes a typical RESTful web service to manage persistence and intricate business logic. To consume this service, I am using Angular 2 with components coded in TypeScript. Instead of developing m ...

Position the div within a flex container to the right side

How can I position the album cover div to the right within the card? I attempted using the align-self: end property, but it did not work. Can someone please assist? .card { border: 1px red solid; width: 450px; height: 150px; border-radius: 5px; ...

Dynamically fetching data with Node.js using Ajax requests

Despite my efforts to scour Google and Stack Overflow, I have been unable to find a reliable method for posting data to my Node.js server. I've noticed conflicting information on various methods, likely due to changes over time. One particular code ...

Arrange the elements within the anchor tag in a vertical line using Angular Material

Is there a way to style the elements within an anchor tag in Angular Material so that they display in a single line? I am looking for the CSS solution to achieve this outcome. This is my current HTML code: <a mat-list-item [routerLink]="['das ...

Learning how to utilize localStorage in conjunction with a color picker to modify the --var of :root

After spending an entire afternoon on my JavaScript issue as a beginner, I have a specific goal in mind: allowing the user to select and change the main color of the page. To implement this, I inserted a color picker in my HTML: <input type="color ...

Sending the content of a textBox to a JavaScript function

Although my question may be somewhat outdated, after exhausting all possible solutions, I am still unable to find a resolution. I am trying to create a function for verifying input fields: This particular function is functioning correctly: function myFu ...

What are some creative ways to customize the background of a MaterialUI modal design?

I am currently facing an issue with my MUI Modal setup. The backdrop is displaying as black even though I have proper styling in place. No matter what I do, the backdrop remains pitch black and does not update. Below is the code snippet along with a scree ...

Prevent automatic scrolling of a div in jQuery while it is being hovered over

After addressing a previous question, I have further refined the script but encountered an issue at the final step. The current functionality involves a div automatically scrolling 50px at a time until it reaches the bottom, then it scrolls back to the to ...

Remain on the React page following an HTTP POST request to Flask

Is there a way to stay on the React frontend (localhost 3000) after completing a POST request to a Flask backend from a React form, without redirecting in the Flask backend? Relevant code: export const DateForm = () => { return ( <div&g ...

Display the specified div element automatically

Despite my best efforts, I can't seem to figure this out. I've searched everywhere for a solution but no luck so far. Is there a way to automatically display the content from http://tympanus.net/Tutorials/CSS3ContentNavigator/index.html#slide-mai ...

What strategies can I use to streamline this array update function code?

Looking to simplify my updated array function. The update function involves updating and comparing values in an array. The comparison will be done within the fruit_temp. For example, data in fruit_temp's fruit_db_id corresponds to an existing id in th ...