点击提交时无法检查空或null。
当按空格键时,空值将被发送到数据库。
按钮可以提交传递数据到数据库
请帮忙。
<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 : <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef9797979797af88828e8683c18c8082" rel="noreferrer noopener nofollow">[email protected]</a></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>
无法检查数据是否为空
当按空格键时,空值将被发送到数据库。
按钮可以提交传递数据到数据库
请帮忙。
最佳答案
数据“”是有效数据,但它是空的,而不是 null。您可以在将其保存到数据库之前通过 trim
函数进行检查。
例如:
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
更多详细信息:trim
关于node.js - 点击提交时无法检查为空或为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783688/