Elavarasan M
Elavarasan M

Reputation: 192

Disable the button when fields are empty in Angular5 [ invalid form ]

I am learning Angular5, new to this.

I have two input fields, one button. Handled validations for that two fields, will enable the button once two fields are entered. I have disable the button when the form is invalid. But it is not working.

<form class="customerRegisteration-form">  
  <div class="form-row">
  <div class="form-group col-md-6">
    <label for="firstName">First Name</label>
      <input required ngModel name="firstName" id="firstName" class="form-control"  placeholder="First Name" #firstName="ngModel"/>
      <div class="alert alert-danger" *ngIf="firstName.touched && !firstName.valid">First Name is required</div>
  </div>
  <div class="form-group col-md-6">
    <label for="lastName">Last Name</label>
    <input required ngModel name="lastName" id="lastName" class="form-control" placeholder="Last Name" #lastName="ngModel"/>
    <div class="alert alert-danger" *ngIf="lastName.touched && !lastName.valid">Last Name is required</div>    
  </div>
  </div>
  <div class="form-group col-md-4">
      <button type="submit" class="btn btn-lg btn-block btn-info" [disabled]="!customerRegisteration.valid">
        <i class="fa fa-floppy-o"></i> Submit</button>
    </div>
</form>

Kindly help me if anything wrong.

Thanks in Advance.

Upvotes: 3

Views: 10131

Answers (2)

Arash
Arash

Reputation: 1826

You can simply add #customerRegisteration="ngForm" in your tag like below

<form class="customerRegisteration-form" #customerRegisteration="ngForm">  

Full code is

<form class="customerRegisteration-form" #customerRegisteration="ngForm">  
  <div class="form-row">
  <div class="form-group col-md-6">
    <label for="firstName">First Name</label>
      <input required ngModel name="firstName" id="firstName" class="form-control"  placeholder="First Name" #firstName="ngModel"/>
      <div class="alert alert-danger" *ngIf="firstName.touched && !firstName.valid">First Name is required</div>
  </div>
  <div class="form-group col-md-6">
    <label for="lastName">Last Name</label>
    <input required ngModel name="lastName" id="lastName" class="form-control" placeholder="Last Name" #lastName="ngModel"/>
    <div class="alert alert-danger" *ngIf="lastName.touched && !lastName.valid">Last Name is required</div>    
  </div>
  </div>
  <div class="form-group col-md-4">
      <button type="submit" class="btn btn-lg btn-block btn-info" [disabled]="!customerRegisteration.valid">
        <i class="fa fa-floppy-o"></i> Submit</button>
    </div>
</form>

Note. I suggest you to read Angular Forms Article as well as you have many questions about angular forms

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222582

Your Form should have a formGroup as

<form  [formGroup]="customerRegisteration">

or

 <form class="customerRegisteration-form" #customerRegisteration="ngForm">

and then,

<button type="submit" class="btn btn-lg btn-block btn-info" [disabled]="!customerRegisteration.valid">

Upvotes: 6

Related Questions