0

I'm trying to check if a value is "null" but i can't figure out how. Here is my code:

 Milestone: any={};
constructor(public toastController: ToastController) {
    this.Milestone.MilestoneType='';
    this.Milestone.date='';
    this.Milestone.Comment='';
  }
onSubmit(){
  var Milestone = JSON.stringify({
    MilestoneType: this.Milestone.MilestoneType, 
    date: this.Milestone.date,
    Comment: this.Milestone.Comment,
    ID: this.current.id});
    
   if( this.Milestone.date=="null"){
     this.DateToast();
   }
   else if( this.Milestone.Commment==='null'){
    this.CommentToast();
  } 
  else{
...
}

The MilestoneType,date and Comment values are from the HTML file:

<ion-segment name="MilestoneType" [(ngModel)]="Milestone.MilestoneType" [ngModelOptions]="{standalone: true}" >
        <ion-segment-button value="Plant" >Plant</ion-segment-button>
        <ion-segment-button value="Fertilizer" >Fertilizer</ion-segment-button>
        <ion-segment-button value="Other" >Other</ion-segment-button>
      </ion-segment>
    </ion-toolbar>
    <div [ngSwitch]="true" >
      <ion-card *ngSwitchCase="Milestone.MilestoneType=== 'Plant' || Milestone.MilestoneType==='Fertilizer' || Milestone.MilestoneType==='Other'">
          <p>Type: {{Milestone.MilestoneType}}</p>
          <label for="date">Date: </label>
          <input required name="date" [(ngModel)]="Milestone.date"  type="date" clearInput="true">
          <br>
        <div class="form-group">
          <label for="Comment" >Comment:</label>
          <input id="comment" name="Comment" [(ngModel)]="Milestone.Comment" clearInput="true">
      </div>

My goal is to check if a value is null .If so, I need to display a message. I tryied the following in the If structure: this.Milestone.date=="null" this.Milestone.date==="null" Milestone.date=="null" Milestone.date==="null"

austin_ce
  • 1,063
  • 15
  • 28

1 Answers1

1

When you put null like "null" then it's a string. If you simply want to check if a field is empty or not then try this

if(!this.Milestone.date){ //empty date field
   this.DateToast();
}
if(!this.Milestone.Commment){//empty comment field
   this.CommentToast();
} 
Anil
  • 121
  • 6