html - 我们如何在输入字段中添加必填字段,必填标签在其中不起作用?
问题描述
我正在尝试在输入类型中添加必填字段,甚至尝试在输入标签中使用 required 属性,但作业未完成。
我尝试在输入标签中使用 required 属性,但它仍然无法正常工作。
<input name="otp" (keypress)="onOTPChange($event)" [(ngModel)]='otp'
class="ms-TextField-field form-control Login_inputWidth form-control Details_inputWidth"
required type="text" value="" id="otp" placeholder="Enter your OTP">
</div>
我希望所需的属性可以工作,但实际上它不起作用。
解决方案
这里举一个简单的例子。请注意无效边框的样式。这些类是由角度设置的。
有关更多信息检查:https ://angular.io/guide/form-validation#template-driven-validation
<style>
.ng-dirty.ng-touched.ng-invalid {
border: solid 1px red;
}
</style>
<input name="fieldotp"
[(ngModel)]='otp'
class="ms-TextField-field form-control Login_inputWidth form-control Details_inputWidth"
required
#fieldotp="ngModel"
type="text"
placeholder="Enter your OTP">
<div *ngIf="fieldotp.invalid && (fieldotp.dirty || fieldotp.touched)"
class="alert alert-danger">
<div *ngIf="fieldotp.errors.required">
OTP required.
</div>
</div>
推荐阅读
- java - 为什么(int i=0;我
关于二维数组的嵌套for循环,为什么应该是(int i=0;i<cars.length;i++),而不是(int i=0;i<=cars.length;i++)?
公共类主要{
public static void mai
- windows - 无法从其他文件夹加载 SQLite 扩展
- python - 自定义 template.yaml 时,部署的 Lambda 中缺少 aws-sam-cli Python requirements.txt
- image - Google Lighthouse:使用错误的 SRCSET 图像
- javascript - 如何使用 agora.io 动态创建频道
- php - 如何让 Wordpress 允许上传 .doc 和 .docx
- python - python中的叉积/笛卡尔积
- python - Arrange rectangle in the background
- sorting - StreamAPI 上的 Flink 高效排序
- java - java.sql.SQLException:找不到适合 jdbc:mysql://localhost:3306/mydatabase Android Studio Java MySQL 的驱动程序