html - CSS无效选择器不适用于反应形式
问题描述
我面临一个问题,因为我有自定义验证,所以我无法将 :invalid css 选择器与我的输入绑定。invalid 仅在基本验证器示例中有效:输入 type="email" 因此无效将与 example@ 或 example@example.exmaple 一起使用,而我的函数仅验证最后一个(example@example.example)。 项目链接
文件
<input class="custom-input" type="email" [(ngModel)]='Email' name="email" placeholder="Email" formControlName="emailTxt">
<div class="err>
this is an error
</div>
打字稿文件
this.signUpForm = this._fb.group({
emailTxt: [null, Validators.compose([
Validators.required
])]
}, {
validators: [
EmailValidation('emailTxt'),
]
});
表单验证功能
export function EmailValidation(controlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
if (control.errors && !control.errors.invalidEmail) {
return;
}
if (control.value.trim().match(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{1,5}|[0-9]{1,3})(\]?)$/) == null) {
control.setErrors({ invalidEmail: true });
}
else {
control.setErrors(null);
}
}
}
CSS 文件
.custom-input:invalid{
& ~ .err {
max-height: 200px;
padding: 0 18px 10px 20px;
color: red;
}
}
我希望每个 control.setErrors() 函数都应该触发无效的选择器
解决方案
在您的项目中添加下面的 CSS 代码,这绝对有效
input.ng-dirty.ng-invalid {
border: 1px solid red;
}
推荐阅读
- mysql - 如果 select(*) 计数值为 2,如何更改此查询以返回 true?
- typescript - 如何在 TypeScript 中保存枚举值?
- typescript - 启动后为 Angular 6 项目获取“错误 TS1005,TS1109”
- uwp - Pointer Released On RichEditBox UWP
- javascript - D3:每 7 年在 x 轴上缩放数据
- ios - 单击按钮时无法播放一系列图像
- sql - 使用 2 列中的数据返回 SQL Server 2016 中的 json 值列表
- shell - 通过 shell 将事件发送到本地数据狗代理
- reactjs - 测试是否已使用 Jest 调用了 React prop 方法
- python - 从 2d numpy 数组创建 3d numpy 数组