0

I have two input boxes, start date and end date, when data in entered, both the values should not be the same, I need a validator for that

then there is an add button, which when clicked adds two more input boxes, these two start date and end date, should not match the above input boxes days Like the example in the photo, skill and exp are my star date and end date respectively

I am having difficulty writing custom validation for it.

example https://stackblitz.com/edit/angular-u8dxok?file=src%2Fapp%2Fapp.component.html

1 Answer 1

2

I let you here one implementation.

https://stackblitz.com/edit/angular-hm7967

And paste the code here anyway.

Template

<div>

<form [formGroup]="formGroup">
<label for="fieldOne" id="fieldOne">Field One: </label>
<input type="text" formControlName="fieldOne"/><br/>
<label for="fieldTwo">Field Two: </label>
<input type="text" id="fieldTwo" formControlName="fieldTwo" />

</form>

<pre>
  {{formGroup.valid}}
</pre>

</div>

Component

    import { Component } from '@angular/core';
import { FormGroup, FormControl, Validators, AbstractControl } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  public formGroup: FormGroup;

  constructor(){
    this.formGroup = new FormGroup({
      fieldOne: new FormControl('', [Validators.required]),
      fieldTwo: new FormControl('', [Validators.required]),
    }, [
      this.customValidation
    ]);
  }

  private customValidation(form: FormGroup){
    console.log(form);
    const fieldOne : AbstractControl = form.get('fieldOne');
    const fieldTwo : AbstractControl = form.get('fieldTwo');
    if (fieldOne.value === fieldTwo.value) {
        return {
          customValidation: true
        }
    }
    return null;
  }

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.