validation - 如何逃避 Angular2 字段验证模式

标签 validation angular

如何将模式正则表达式放置在 angular2 形式中 我有这个图案

@Component({
    selector: 'my-form',
    template: `
        <h2>My Form</h2>
        <form (ngSubmit)="onSubmit(ff)" #ff="ngForm">
            <div>
                <label for="mail">Field</label>
                <input ngControl="Field" type="text" id="email" required #email="ngForm" pattern="[a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$">
                <span class="error" *ngIf="!email.valid">Not valid</span>
            </div>
            <button type="submit" [disabled]="!ff.valid">Submit</button>
        </form>
    `
})

我的模式正则表达式出现错误。有没有办法以某种方式逃脱它?

这是我在该正则表达式上遇到的一些错误:

[tslint] expected an assignment or function call
[tslint] missing whitespace
[ts] Cannot invoke an expression whose type lacks a call signature.
[ts] Cannot find name 'a'.
[tslint] expected an assignment or function call
[tslint] missing whitespace
[ts] Cannot invoke an expression whose type lacks a call signature.
[ts] The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.
[ts] Cannot find name 'a'.
[tslint] missing whitespace
[ts] Expression expected.

enter image description here

最佳答案

` 之前添加 \(反引号)

pattern="[a-z0-9!#$%&'*+\/=?^_\`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$"

或者将模板移动到 HTML 文件并使用 templateUrl 而不是 template

关于validation - 如何逃避 Angular2 字段验证模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36760977/

相关文章:

angular - 运行 Angular i18n 时如何设置 messages.xlf 输出位置?

Angular2 ckeditor 编辑配置

Angular 2 [ngSwitchCase] 不支持负值?

validation - Wpf 组合框限制为列表

jquery - 表单验证 Jquery 正则表达式

java - Jersey 2 bean 验证不起作用

angular - 为什么我可以从@angular/animations 和@angular/core 导入动画组件?

javascript - 在 JavaScript 中使用正则表达式验证首字母缩写

jquery - JS jQuery - 检查值是否在数组中

javascript - Angular 8 - HttpClient 获取复杂的 JSON 对象