Open In App

How to make sure clients have enough words in textarea by using angularjs in order to disable/enable a button?

The Task here is to make sure that the user enters enough words in the Textarea. The button will be enabled or disabled on the basis of the word count entered by the client or user. Here the word limit will be set by admin of the project or Application. If word count lies between the parameter set by Admin then the button will remain enabled. If word count exceeds the limit or is less than the limit then it will remain disabled.

Example: In this approach, we have set parameters to word count between 5 words to 20 words.  



Input : GeeksforGeeks
Output: Button will be disabled
Input : Hello Geek! welcome to GeeksforGeeks!! 
Output: Button will be enabled

For reaching the goal we will use InputEvent. That will help us to get a count of words after the input of every character. It is an Angular event binding to respond to any DOM event. It is an asynchronous event that is triggered as the user is interacting with the text-based input controls.

Below all the steps are mentioned order-wise:



Implementation Example:  




<textarea (input)="CheckLen($event)" id="ta" ></textarea>
<br>
<button id="bt" disabled={{check}}>Button</button>




import { Component, OnInit } from '@angular/core';
 
@Component({
  selector: 'app-txtchk',
  templateUrl: './txtchk.component.html',
  styleUrls: ['./txtchk.component.css']
})
export class TxtchkComponent implements OnInit {
  c=0;  //defined counter
  constructor() { }
  check;  //defined check variable
  ngOnInit(): void {
  }
//value of textarea is taken from event
  CheckLen(event){
 
    // c counts the number of words of input value
    this.c=event.target.value.split(' ').length;
 
    // We have set that minimum word count should
    // be 5 or more and the maximum should be 20.
    if(this.c<5 || this.c>20){       
      this.check=true;
    }
    if(this.c<=20 && this.c>=5){
      this.check=null;
    }
  }
}

Output: Start the Development server and enter words in textarea to see whether the button goes enabled or disabled on particular outputs. Here are few output Examples with word count values logged on console. 

 


Article Tags :