Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

141
Views
Why does the (change) function work only if I leave for first time?

I have written this code to check if the total words are atleast 3. I tworks when I put less than 3 words and leave text box by clicking outside or tab. but it only works first time if I leave 2nd time it doesn't hit i.e. no error. So i have to go again and make sure words are less than three and leave. How to fix it?

countTotalWords() {
    let str= this.model.FullName_AR;
    
    if(!str) {
      return;
    }

    let totalWords= str.split(' ')
           .filter(function(n: string) 
           { 
             return n != '' 
           })
           .length;

      if(totalWords < 3)
      {
        this.wordCountAchieved= false;
        Swal.fire(
         'minimum 3 words',
         'input minimum 3 words',
         'error'
        )
      }
      else {
        this.wordCountAchieved= true;
      }
  }

html

 <input type="text" #nameArabic (change)="countTotalWords()" class="form-control" id="nameAR" [(ngModel)]="model.FullName_AR" name="nameAR" #name="ngModel" required>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Try making a change and see what happens the second time round because change only triggered when it detects a change to the content.

about 4 years ago · Juan Pablo Isaza Report

0

you need to use blur event insted of change

 <input type="text" #nameArabic (blur)="countTotalWords()">
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!