I can't check the empty or null when I click submit

Viewed 241

I can't check the empty or null when I click submit.

When pressing the spacebar, the empty value will be sent to the database.

button can submit pass to sent data to database

Please help.

  <form #Register="ngForm">
  <div style="width: 500px">

    <br/>
    <h4>New Reegister</h4>
    <br/>

    <div class="form-group">
      <dt><label for="name">Name</label></dt>
      <input [(ngModel)]="name" name="name" type="text" class="form-control" placeholder="input your name" data-bv-notempty-message="The name is required" required>
    </div>

    <div class="form-group">
      <dt><label for="lastname">LastName</label></dt>
      <input [(ngModel)]="lastname" name="lastname" type="text" class="form-control" placeholder="input your lastname" data-bv-notempty-message="The lastname is required" required>

    </div>

    <div class="form-group">
      <dt><label for="email2">E-mail</label></dt>
      <input [(ngModel)]="email2" name="email2" type="text" class="form-control" placeholder="input your email" data-bv-notempty-message="email" required [pattern]="emailRegex" >
      <div class= "p-2 text-danger"> <small>* Require Example : xxxxx@gmail.com</small> </div>
    </div>

    <div class="form-group">
      <dt><label for="password2">Password</label></dt>
      <input [(ngModel)]="password2" name="password2" type="password" class="form-control" placeholder="input your password" minlength="4" required>
      <div class= "p-2 text-danger"> <small>* Enter atleast 4 characters</small> </div>
    </div>
  <!--
    <div class="form-group">
        <label for="password3">Password Confirm</label>
        <input [(ngModel)]="password3" name="password3" type="password" class="form-control" placeholder="input your password" required>
    </div>
    -->

    <div class="form-group">
        <dt><label for="passwordhint">Password Hint</label></dt>
        <input [(ngModel)]="passwordhint" name="passwordhint" type="password" class="form-control" placeholder="input your password" minlength="4" required>
        <div class= "p-2 text-danger"> <small>* Enter atleast 4 characters</small> </div>
      </div>

    <button type="submit" type="reset" class="btn btn-danger" [disabled]="!Register.valid" (click)="Regis()">Save</button>
    

  </div>
  </form>


cannot check data null

When pressing the spacebar, the empty value will be sent to the database.

button can submit pass to sent data to database

Please help.

2 Answers

Data " " is a valid data but it's empty, not null. You can make a check by trim function before saving it into database.

For example:

let str = ' ';
console.log(str.length) // Length = 1
str = str.trim();
// Here str = '' and you can check length of it
console.log(str.length) // Length = 0

More detail: trim

This has to go in the submit button.

In this case if the variable 'name' contain only spaces the function 'trim' will reduce its length to 0.

So the database call will happen only if the variable 'name' has a value.

this.name = this.name.trim();
if(name.length) {
    // do the database call
} else {
    // prompt a message to let the user know that the particular field is empty
}
Related