1

Instead of your name is required I want that animated pulse classes should be added to the input field. Both these classes are from animate.css library which displays pulse effect and the class should also be removed once the user enters a valid text to reset the animation so that the animation shows again if the user does something wrong on that input field agin.

<form method="post" [formGroup]="registerForm" (ngSubmit)="onSubmit()">
    <input type="text" formControlName="first_name" name="first_name" placeholder="First Name *"/>
    <div *ngIf="registerForm.controls.first_name.errors && registerForm.controls.first_name.touched" class="error">
    <div *ngIf="registerForm.controls.first_name.errors.required">Your name is required</div>
    </div>
     <div *ngIf="submitted && registerForm.controls.first_name.errors && registerForm.controls.first_name.pristine" class="error">
    <div *ngIf="registerForm.controls.first_name.errors.required">Your name is required</div>
    </div>

.ts

    this.registerForm = this.formBuilder.group({
          first_name: ['', Validators.required],

 onSubmit() {
    this.submitted = true;
    if (this.registerForm.invalid) {
        return;
    }
}

1 Answer 1

1

You can use ngClass directive to dynamically add or remove class to html tags. Based on the code you provided i am guessing you want to add class when input field is touched. You achieve this with reactive form control property touched and invalid.

<input [ngClass]="{'animated pulse': registerForm.get('first_name').touched && registerForm.get('first_name').invalid}" type="text" formControlName="first_name" name="first_name" placeholder="First Name *"/>

Sign up to request clarification or add additional context in comments.

1 Comment

To improve the quality of your answer, could you please add some commentary as to what this code does and how it solves the original question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.