Steps to activate a button once the input field has been completed

https://i.sstatic.net/l6mUu.png

It's noticeable that the send offer button is currently disabled, I am looking to enable it only when both input fields are filled.

Below, I have shared my code base with you.

Please review the code and make necessary modifications on stackblitz

1. example-dialog.component.html

<form id="bidForm">
  <div class="form-row">
    <div class="form-group col-md-6">
      <label for="inputQuantity">Quantity</label>
      <input
        type="number"
        name="quantity"
        class="form-control"
        id="inputQuantity"
        placeholder="Quantity(QTL)*"
      />
    </div>
    <div class="form-group col-md-6">
      <label for="inputPrice">Price</label>
      <input
        type="number"
        class="form-control"
        id="inputPrice"
        placeholder="Price(₹/QTL)"
      />
    </div>
  </div>

  <button
    type="submit"
    class="btn btn-primary btn-lg btn-block"
    disabled="{{ buttonDisabled }}"
  >
    Send offer
  </button>
</form>

2. example-dialog.component.ts

import { Component, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";;

@Component({
  selector: 'app-example-dialog',
  templateUrl: 'example-dialog.component.html',
})
export class ExampleDialogComponent {
// here I attempted logic implementation but realized it's incorrect,  
buttonDisabled: boolean;
  ngOnInit() {
    this.buttonDisabled = false;
  }

  constructor(
    public dialogRef: MatDialogRef<ExampleDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: any) { }


  onNoClick(): void {
    this.dialogRef.close();
  }


}

Answer №1

Ensuring proper form validation is crucial. To begin with, include required attributes in your input fields. Next, make sure to disable the submit button when the form is invalid.

<form id="bidForm" #bidForm="ngForm">
    <div class="form-row">
        <div class="form-group col-md-6">
            <label for="inputQuantity">Quantity</label>
            <input
                type="number"
                name="quantity"
                class="form-control"
                id="inputQuantity"
               placeholder="Quantity(QTL)*"
               required
             />
        </div>
        <div class="form-group col-md-6">
           <label for="inputPrice">Price</label>
           <input
              type="number"
              class="form-control"
              id="inputPrice"
              placeholder="Price(₹/QTL)"
              required
           />
       </div>
   </div>

   <button
      type="submit"
      class="btn btn-primary btn-lg btn-block"
      [disabled]="!bidForm.form.valid"
  >
     Send offer
  </button>
</form>

Preview it live here.

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

Waiting for nested observables to complete in Angular 2

Before proceeding to another page in my Angular app, I need two nested Observables to complete. However, I am facing synchronization issues as they are nested within each other. These Observables are initialized in my authentication service. authentication ...

Material Design Angular2 Toolbar

I am currently working on designing a toolbar using Material and Angular2. My main challenge is formatting the toolbar in such a way that the search bar appears centered in white, while the "Create Project" button and other buttons are aligned to the right ...

What is the reason for Object.keys not returning a keyof type in TypeScript?

Wondering why Object.keys(x) in TypeScript doesn't return the type Array<keyof typeof x>? It seems like a missed opportunity as Object.keys outputs that by default. Should I report this on their GitHub repo, or should I just submit a pull reques ...

Navigating back to previous page with the help of Authguard

I am looking to incorporate a redirection feature where, if a user is logged in, they should be directed to the previous page. For example, from Page A to Login (successful) back to PageA. I have tried using the router event subscribe method for this purpo ...

Achieving vertical alignment of form controls with labels that wrap in a row using Bootstrap 4

I am facing an issue where input elements in a row with labels above them are causing alignment problems when the screen width is reduced. The longer labels wrap to the next line and push the input element down, increasing the overall height of the row: h ...

In what scenarios would it be more advantageous to utilize ngStyle over creating a custom directive?

I had the need to dynamically change the width of a column, so I created a unique custom directive for that specific purpose: @Directive({ selector: '[rq-column-size]' }) export class ColumnSizeDirective { @Input('rq-column-size') ...

How to Update Angular 10 ESRI Map Layers Without Reloading the Entire Map

Recently, I successfully developed an Esri Map using Angular 10 where users can toggle different map layers by selecting from a group button. The input for selecting layers is an array, for example: ['0', '1', '2'], with each ...

When employing CDK to configure an SNS topic Policy Statement with actions limited to ["sns:*"], the CloudFormation output may display a warning message stating "Policy statement action is not within the service scope."

Encountering an issue when attempting to reference all SNS actions with * in CDK. const MyTopicPolicy = new sns.TopicPolicy(this, 'MyTopicSNSPolicy', { topics: [MyTopic], }); MyTopicPolicy.document.a ...

execute npm scripts concurrently

Seeking a simpler solution for managing pre-script hooks in my package.json file. Currently, I have multiple commands that all require the same pre-script to run. While my current implementation works, I'm wondering if there is a more efficient way to ...

Creating a Personalized Color Palette Naming System with Material UI in TypeScript

I have been working on incorporating a custom color palette into my material ui theme. Following the guidance provided in the Material UI documentation available here Material UI Docs, I am trying to implement this feature. Here is an excerpt from my cod ...

Is there a compelling case for implementing Meteor in 2017?

Back in the day, Meteor was expected to revolutionize web development on node by simplifying the process of creating interactive applications. Though I'm not well-versed in its history, it seems like most of the development effort has shifted elsewher ...

In Typescript, a function that is declared with a type other than 'void' or 'any' is required to have a return value

I'm a beginner in Angular2/Typescript and I am encountering an error while trying to compile my project: An error is showing: A function that has a declared type other than 'void' or 'any' must return a value. Here is the code sn ...

What steps should be taken to resolve the error message "This Expression is not constructable"?

I'm trying to import a JavaScript class into TypeScript, but I keep getting the error message This expression is not constructable.. The TypeScript compiler also indicates that A does not have a constructor signature. Can anyone help me figure out how ...

Confirm that the email input field in the form is accurately validated

Is it possible to validate multiple email addresses separated by commas using react-hook-form with a Material-UI text field? Initially, the input field only allows for a single email address to be entered and validated. However, I would like the user to b ...

Can data be transmitted to two separate scripts simultaneously?

Just a quick HTML inquiry: Can data from HTML forms be sent to two separate scripts using the "action" attribute, or is there an alternative method? I'd like to explore other options aside from utilizing curl. ...

Add a class individually for each element when the mouse enters the event

How can I apply the (.fill) class to each child element when hovering over them individually? I tried writing this code in TypeScript and added a mouseenter event, but upon opening the file, the .fill class is already applied to all elements. Why is this ...

The Append method in FormData is not functioning properly within Angular

I'm facing a challenge in my Angular application where I need to enable image uploads to the server. The issue arises when attempting to add a selected file to the newly created FormData object. Below is the relevant TypeScript component snippet: ima ...

Angular CLI "serve" encountered a 404 status code when using CURL, yet the webpage is loading successfully in the browser

As I develop a new application using angular2 within Electron, I always aim to automate every aspect of the process. To achieve this, I have implemented various NPM scripts for serving, building, and packaging within the Electron wrapper. However, I am cur ...

Using the `ngrx` library to perform an entity upsert operation with the

I am facing a certain challenge in my code. I have an action defined as follows: export const updateSuccess = createAction('Success', props<{ someId: string }>()); In the reducer, I have an adapter set up like this: export const adapter: ...

Navigating in express

Here is the structure I am working with: server.ts routes/ index.ts homeRoute.ts In server.ts: let app = Express(); app.use(router); In routes/index.ts: const routes = Router(); export default function router() { routes.use('/home' ...