It appears that the act of clicking is being hindered by a state

I am using directives to validate user input upon blur events. You can view the code in action on this plunkr:

http://plnkr.co/edit/avEJ2xfLfz6ihM3DwLyB?p=preview (The first field validates email addresses and the second field takes 'yes' as valid input)

While the validation is working, there seems to be an issue where the allowValidation() function needs to be triggered twice when clicking the submit button.

After some investigation, I found that there might be a conflict between the blur event and the button click event; it appears that when one of the text fields has an ongoing blur event, the button only registers the blur instead of the click.

Is there a way to prioritize the click event over the blur event? I am calling the validation function on both events.

Answer №1

During my testing, I didn't encounter any issues with your code. However, if you come across a situation where the blur event and button click conflict, one solution could be to use a variable to track when validation is triggered and reset it once validation is complete.

Here's how you can implement this in the linking function of your directive:

// Keep track of validation in progress
var validationInProgress = false;

function doValidation() {
    validationInProgress = true;
    // Validation logic here
    if (EMAIL_REGX.test(elm.val())) {
        ctrl.$setValidity('emails', true);
    } else {
        ctrl.$setValidity('emails', false);
    }

    // Reset after validation is done
    // This should be the last statement in this function
    validationInProgress = false;
}

Once this setup is complete, before calling doValidation(), check if there is already a validation in progress:

elm.bind('blur', function () {
    if (!validationInProgress) {
        scope.$apply(doValidation);
    }
});

scope.$on('kickOffValidations', function () {
    if (!validationInProgress) {
        doValidation();
    }
});

For reference, here is a link to a Plunkr showcasing the updated code: Plunkr.

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

using a single controller to manage multiple pages

I created a website with routes using $routeProvider. For each page, I created a controller that looks like this: myApp.config(function ($routeProvider) { $routeProvider .when('/category/Web development', { ...

Error: The webpage you were looking for in the lobby cannot be found due to a django-channels routing

routes.js from channels import include, route from chat import consumers from . import game_consumers channel_routing = [ #game routing route('websocket.connect', game_consumers.ws_connect_lobby, path=r"^/lobby/$"), route('webs ...

The Chrome browser allows interaction between two separate divs, where a button located in one div can impact

One issue I encountered involves a button located within a div that has unintended consequences for another div. Here is the basic structure of the elements: <div> <div> <div> <div> <butto ...

What is the best way to retrieve an ng-model parameter within a controller?

I'm working on implementing a PUT method in my controller and need to bind the new value back to it. I've tried: <div ng-app="myApp" ng-controller="personCtrl"> <form> First Name: <input type="text" ng-mod ...

Eliminating the dynamic element in jQuery causes disruption to the ViewContainerRef container

In my Angular 2+ application, I am dynamically creating components using the following code snippet: @ViewChild("containerNode", { read: ViewContainerRef }) cardContainer; const factory = this.ComponentFactoryResolver.resolveComponentFactory(CardComponen ...

Is there anyone out there who has successfully imported a model from the three.js editor?

As a designer and 3D artist who occasionally delves into coding, I have a question regarding loading a model from the three.js editor into an actual web page. Despite my limited programming knowledge, I've tried various methods to achieve this, includ ...

Postpone an automatic action in React

I'm currently working on adding a fade out animation to a page change in React, but I need to delay the click event before the page actually transitions. Here's the code snippet I have so far: export default function Modal({setTopOf}) { const ...

Has every AJAX async request been completed successfully?

I am working with nested ajax requests. The initial request retrieves a list of devices, and for each device, I make another ajax request to retrieve additional data. Once the nested request is successful, I append the data to a <table>. However, I ...

What are the steps to create a class diagram for a NodeJS application?

For my final year project, I am looking to develop an application using Node API. As we delve into drawing the class diagram, it occurs to me that unlike Java or C#, Node JS does not have a built-in class concept. What would be the most effective approac ...

Puppeteer will not navigate to chrome://version when running in headless mode

Currently, I am utilizing the puppeteer.connect method to navigate to chrome://version in order to extract the user-agent being used by Puppeteer. Everything works fine when headless mode is disabled, but an error occurs when attempting it with headless mo ...

React JS - Sending props from Dev and Build to App component

Looking to include static assets and props in my App, specifically having image assets set with a base64 string in the build process. Want to ensure these assets are accessible to the App's props before development and build stages, similar to the fun ...

Can you explain the functioning of knockout container less syntax? (does it have any drawbacks?)

There are numerous instances and examples of using knockout ContainerLess syntax, although I find it challenging to locate proper documentation from their site. Initially, my question was "is it evil?" but upon realizing my lack of understanding on how it ...

Place a Three.js scene within a jQuery modal dialogue box

I am attempting to integrate a Three.js scene into a jQuery modal window. The objective is to utilize the Three.js scene in a larger window size. This scene should be displayed after clicking on an image that represents the scene in a smaller dimension. Y ...

What is the method for retrieving an attribute's value from an object that does not have key-value pairs?

My current project involves working with dynamoose and running a query that produces the following output: [ Document { cost: 100 }, lastKey: undefined, count: 1, queriedCount: undefined, timesQueried: 1 ] When I use typeof(output), it returns O ...

Creating bulk entries with associated tables in Sequelize and ensuring that duplicates are not created

There are 2 models in my project - Actors and Movies, with a BelongsToMany association. const Movie = sequelize.define( MOVIES, { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, }, title: { ty ...

Decode a JSON string that has already been encoded

Currently, I am dealing with JSON strings in which the quotes are not properly escaped. The strings are structured like this: { "foo" : "hello my name is "michael"" } Is there a practical way in JS/PHP to escape the quotes within the value without manual ...

What impact does the size of HTML have on my JavaScript (especially in relation to page loading speed)?

During the development of my website, I used unordered lists for navigation. However, I recently stumbled upon an article from the Yahoo developer blog that provided valuable insights on optimizing loading times through various performance enhancement tech ...

Is it possible to use a full-width material-ui Button inside a Badge component?

Within a grid, I had initially used fullWidth on a Button to make it expand and fill the container. Everything was functioning correctly until I enclosed the Button in a Badge element. Now, the fullWidth property is not being applied, and the button rever ...

Is there a different option besides using the scale() function in CSS for transforming elements

It's a well-known fact that elements with the CSS property position: fixed don't behave as expected when the container has the transform property applied in CSS. After searching through various threads, I couldn't find a definitive solution ...

Issue with reflect metadata in Next.js edge runtime causing functional problems

Currently, I am utilizing a package in my upcoming 13 app that incorporates reflect metadata. Unfortunately, during the next build process, an error occurs for which I haven't been able to find a solution. ../../eshop-sdk-js/node_modules/reflect-metad ...