javascript - 如何在按钮单击时验证输入不保存 0 或小于 0?
问题描述
我想通过角度 6 中的输入类型号保存 Length。但长度不应为 0 或小于 0 。当有人输入 0 或小于输入值时,它应该在按钮单击时显示错误。我尝试添加 min="1" 但它不起作用
<mat-form-field class="example-full-width">
<input matInput type="number" [(ngModel)]="ItemModel.Length" placeholder="Length" min="1"
id="Length" name="Length" required>
</mat-form-field>
解决方案
You can set your min value to 0.1 and add ngclass for red border
<mat-form-field class="example-full-width">
<input matInput [(ngModel)]="ItemModel.Length" type="number" name="Length"
placeholder="Length" [ngClass]="{ 'red': ItemModel.Length <= 0 &&
greaterthenzero }" min="0.1" required>
</mat-form-field>
on submit button you can check your model value and add one boolean property and set to true if value is less than 0
submit() {
this.greaterthenzero = false;
if (this.ItemModel.Length <= 0) {
this.greaterthenzero = true;
}
}
推荐阅读
- jquery - MVC 5 Razor View - 在 foreach 循环中加载部分视图,需要将显示/隐藏切换设置为每个部分
- string - Bash / Shell脚本在读取文件时将字符串与字段参数连接在while循环中
- vim - 是否可以在防火墙后面使用 coc.nvim 扩展?
- php - WC_Order->get_items() 返回空项目
- c# - 在 SSMS 中查询返回结果,但在 C# 中没有
- google-sheets - 存在匹配项时将单元格值合并到一个单元格中
- javascript - 将 IndexedDB 实例作为 Electron 中的不同网站访问
- visual-studio-code - 在 Visual Studio Code 的工作区中展开文件?
- here-api - 当路由 API 没有问题时,HERE Fleet Telemetics API 找不到路由?
- c# - 7 Zip 提取错误:标头错误 密码错误