Learn how to use Angular2 to disable a button and display an error message from the response after a single click

I have a submit button that triggers 2 APIs when clicked. If the input box is empty, the submit button is disabled. Now I want to implement two conditions:

If an error message is received in the response stating,
Email-Id you provided does not exist in the medicamind account,

then the submit button should remain disabled until a correct email is entered.

  1. After entering an email and clicking on the save button, it should be disabled after one click. If editing is done in the input box, it should either enable or remain disabled.

Answer №1

To control the button's status, utilize the disabled attribute:. Toggle it to true if an error occurs, and switch it back to false once the correct response is received. In the scenario where you need to trigger generateEmailOtp(enterSms, enterEmail); booleanVar = false on click, simply include this code snippet. Hopefully, this information proves useful!

Answer №2

To manage multiple events, it is important to validate certain aspects. Here are the key things to consider:

1- Email Validation: 2- OnClick Trigger: Implement a method that disables the action once executed.

Email validation can be done in real-time as the user inputs data. For template-driven forms, utilize the keyup event with regex for email validation such as this example:

<!-- HTML File -->
<input 
   type="text" 
   [(ngModel)]="inputData" 
   (keyup)="keyUpValidation()" >

<button 
   [disabled]="disableButton"
   (click)="executeApiCall()">

// .ts File
inputData: string;         // stores input data
disableButton: boolean = false; // initial state as disabled

// Triggers on user input
keyUpValidation() {
   if (this.validateEmail(this.inputData)) {
      this.disableButton = true; // enables button
   } else {
      // handle exceptions here
   }
}

// Email validation logic
validateEmail(email) {
   // implement email validation
   return true or false based on validation result;
}

// Executes on button click
executeApiCall() {
   this.disableButton = false;  // Disables the button after action
   // perform additional actions after button disable
}

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

In search of a JavaScript code that will automatically redirect a child page to an iframe located within the parent page

Hi everyone, I'm a newcomer to the world of programming and I could really use some help. Currently, I am working on a project using Iframes. I understand that Iframes may not be the most ideal choice, but I haven't been able to find a suitable r ...

Headers cannot be sent to the client after they have already been set in Axios within Next.js

For additional discussion on this issue, please refer to the GitHub thread at - https://github.com/axios/axios/issues/2743 In my Next.js project, I am using Axios and occasionally encounter an error related to interceptors when returning a Promise.reject. ...

How do I verify if a boolean variable has been changed within an Angular service?

In my MatHorizontalStepper parent component, I utilize the subscribe function to monitor changes in an Observable within a service. The purpose is to automatically progress to the next step in the stepper when the observable switches from false to true. D ...

Is it possible to alter the css twice through the action of clicking on two individual buttons

One feature I have in my interface allows users to click on the edit button to bring up borders and change the background color of other divs. Once the edit button is pressed, it is replaced by cancel and save buttons. The goal is for the borders and backg ...

What is the best way to navigate to the bottom of a page when new data is added?

I've created a chat feature in my Ionic app, but the issue is that when a user receives a new message while being on the chat screen, the view doesn't automatically scroll down to show the new message. The user has to manually scroll down to see ...

Deleting an element from a JavaScript array

I have a collection of javascript functions that manage intervals by setting and clearing them. The intervals are stored in an array called intervals[]. config: { settings: { timezone: 'Australia/Perth,', base_url: location.p ...

What is the best way to incorporate JavaScript files into a Java application?

Java applications execute Javascript code. However, the Jquery library is too lengthy to be accommodated in a String variable. I am able to read jquery.js from a file but unsure of how to bundle it within a .jar file. ...

What is the process for sorting Google Map markers with AngularJS?

.controller('MapCtrl', ['$scope', '$http', '$location', '$window', '$filter', '$ionicLoading', '$compile','$timeout','$ionicPopup', function ...

Maintain text while transitioning to another page

On my webpage (refer to image for details), users need to select options through a series of steps. The process involves clicking on a link in the top box, then entering a search term in the searchbox to display relevant links in div1. Clicking on a link ...

Tips for declaring a data variable in Vue with the help of APIs

I'm currently working on a project where I am integrating movie data from an API (TMDb) using Vue Routers. I have a component named 'MovieList' where I am fetching data from the API. Below is the data structure: data() { return { mov ...

Tips for accessing the HTML content enclosed within a specific HTML tag using Python with Selenium

I am trying to extract the source code of an HTML document that is embedded within an <iframe> tag generated by JavaScript. The HTML contents within this <iframe> tag appears as #document, which expands to reveal a full HTML document starting w ...

Is it possible to use JQuery ScrollTop while in the midst of an animation?

I am facing an issue with resizing containers and scrolling to elements. I have 2 containers that change width dynamically, each containing clickable elements. When a container is clicked, it animates the resize. However, when a clickable element is clicke ...

Example of material-ui-pickers: Error - Object(...) is not a function

Trying to implement a basic material UI pickers example using a clean project and following the installation and usage instructions provided here: An error message is displayed as follows: TypeError: Object(...) is not a function Module../node_modules/@m ...

MySQL Entry Update Failure

This is the HTML/EJS code snippet: <div class="Edit-Panel" style="display: none;"> <div class="Edit-Wrapper"> <div class="Editing"> <p class="Edit-Header ...

Tips for storing an httpOnly cookie containing a JWT token within an angular 12 application

I am implementing a Single Sign-On (SSO) integration that provides a JWT token. However, I am hesitant to store this token in local storage due to security concerns. Is there a way to securely manage this token without repeatedly fetching it from the ser ...

Executing a Click Action on a Button Using Protractor in AngularJS

Currently, I am in the process of automating a website using protractor. After coding a few steps, I encountered an error when trying to click on a button. The error message indicated that more than one element was found for the locator by.partialButtonT ...

Type '...' does not contain the specified conditional generic property '...'

TypeScript Playground type Foo<BooleanParameter extends boolean> = { boolean_parameter: BooleanParameter } & (BooleanParameter extends true ? { first_parameter: number } : { second_parameter: string }) const bar = <BooleanParameter extends bo ...

Experience a seamless transition as the background gently fades in and out alongside the captivating text carousel

I have a concept that involves three background images transitioning in and out every 5 seconds. Additionally, I am utilizing the native bootstrap carousel feature to slide text in synchronization with the changing background images. Solving the Issue ...

The JSON data retrieved from the API is showing as undefined for the property

Currently, I am working on a project that involves utilizing the Shopify API to fetch products and related data from a store in JSON format. While I was able to successfully retrieve the JSON object from the API, I encountered an issue when trying to acces ...

AngularJS - Issues with Firebase resetPassword not showing updated changes

When incorrect email addresses are entered and the form is submitted, the error message does not display for the user. However, if additional characters are appended to the existing entries, the message appears on the screen. In debugging, the variable $sc ...