0

Here I have one input field when I click on SAVE button it goes in saveNewCategory() function and check it is null or not, but when I put only spaces in input field that time also it is saved so how it is not allowed to save input field when only spaces is given in the input field?

category.component.html

<div>
  <form method="post" enctype="multipart/form-data">
    <mat-form-field>
     <input matInput placeholder="Category Title" maxlength="30" name="categorytitle" [(ngModel)]="this.categoryObj.categorytitle" required>
    </mat-form-field>
  </form>
  <button mat-raised-button color= "accent" (click)="saveNewCategory()">SAVE</button>
</div>

category.component.ts

saveNewCategory(){
  if(this.categoryObj.categorytitle != ''){
    const formData = new FormData();
    formData.append('categorytitle',this.categoryObj.categorytitle);
    this.categoryService.saveNewCategory(formData).subscribe(
      (data) => {
        if(data != undefined && data.status == 1){
          this.snackBar.open('New Category Saved Successfully..!!', '',{
            duration: 2000
          });  
        }
      }
    )
  }else{
    this.snackBar.open('Category Image is required..!!', '',{
      duration: 2000
    });  
  }  
} 
3

1 Answer 1

1

You can use trim method as:

saveNewCategory(){
  if(this.categoryObj.categorytitle.trim() != ''){ <===== Here
    const formData = new FormData();
    formData.append('categorytitle',this.categoryObj.categorytitle);
    this.categoryService.saveNewCategory(formData).subscribe(
      ...
    )
  }else{
    ...
  }  
} 
Sign up to request clarification or add additional context in comments.

Comments

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.