mardi 26 juin 2018

How to Validate Checkbox in angular 5

i am trying to validate my checkbox in a form with different fields, but the problem i am getting is that

html code:

 <div class="form-group">
          <label class="login_label">Courses</label>
          <span style="color:#00bfff">*</span>
          <input [ngModelOptions]="{standalone: true}" [(ngModel)]="courses_mba" type="checkbox" class="" value="mba">Mba
          <input [ngModelOptions]="{standalone: true}" [(ngModel)]="courses_btech" type="checkbox" class="" value="btech">Btech
          <input [ngModelOptions]="{standalone: true}" [(ngModel)]="courses_mtech" type="checkbox" class="" value="mtech">Mtech
          </div>

Ts Code:

if (this.jobForm.invalid && (this.courses_mba === undefined || this.courses_btech === undefined || this.courses_mtech === undefined)) {
  this.snackBarService.requiredValue(' Please complete the form');
} else {
  this.job_courses = this.courses_mtech ? 'mtech' : '' + this.courses_btech ? 'btech' : '' + this.courses_mba ? 'mba' : '';
  this.snackBarService.requiredValue(' form Submitted Successfully');
  console.log('CArray', this.job_coursess);
  console.log('Course', this.job_courses);
  console.log('mba', this.courses_mba);
  console.log('mtech', this.courses_btech);
  console.log('btech', this.courses_mtech);

i am trying to display whose are checked should be display on console by i am not getting the proper output, even the checkbox are not selected the "job_courses" is showing the "btech" i tried to check by check mba and btech its giving me random value i.e sometime btech sometime mtech. What i am expecting is that what i checked should be display in console.




Aucun commentaire:

Enregistrer un commentaire