Validation 如何退出angular2字段验证模式
如何以angular2形式放置模式正则表达式 我有这个图案Validation 如何退出angular2字段验证模式,validation,angular,Validation,Angular,如何以angular2形式放置模式正则表达式 我有这个图案 @Component({ selector: 'my-form', template: ` <h2>My Form</h2> <form (ngSubmit)="onSubmit(ff)" #ff="ngForm"> <div> <label for="mail">Field
@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>
`
})
在
`之前添加一个\
`(反勾选)
或者将模板移动到HTML文件并使用
templateUrl
而不是template
@GünterZöchbauer刚刚更新您是否尝试将HTML移动到其自己的文件并使用templateUrl
而不是template
?哪种方式。看起来图案有问题。我只是将这个regexpattern=“^\S+@\S+$”
用于测试,它也不起作用。该字段始终无效您是否尝试将HTML移出*.ts
文件?是否在浏览器中看到模式
属性?它与源文件中的相同吗?在模式中使用未替换的反勾号很明显。您可以使用反勾号创建多行字符串,但在字符串unscaped中使用相同的引号无法工作。我不知道为什么当你添加``时它不起作用。它似乎正确地将模式传递给HTML。你能让它在这个垃圾桶里繁殖吗?
[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.
pattern="[a-z0-9!#$%&'*+\/=?^_\`{|}~.-]+@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*$"