我正在使用 ngx-chips 将电子邮件列表作为标签添加到输入中。验证器确保每个标签看起来像一封电子邮件。
我怎样才能确保:
1) 验证器仅在添加标签时触发(即用户按回车键、空格或逗号)
2)如果在输入/空格/逗号时电子邮件无效,则该值仍然存在(即,它不清除......以便用户可以修复它)
堆栈闪电战在这里:https ://stackblitz.com/edit/ngx-chips-example-2qdudc
以下是我的电子邮件验证器:
public validators = [ this.must_be_email ];
public errorMessages = {
'must_be_email': 'Please be sure to use a valid email format'
};
private must_be_email(control: FormControl) {
var EMAIL_REGEXP = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,3}$/i;
if (control.value.length != "" && !EMAIL_REGEXP.test(control.value)) {
return { "must_be_email": true };
}
return null;
}
下面是标签:
<tag-input [(ngModel)]='emails'
name="emails"
#email="ngModel"
[errorMessages]="errorMessages"
[validators]="validators"
[editable]='true'
(onTagEdited)="onTagEdited($event)"
[separatorKeyCodes]="[32,188,186,13,9]"
[placeholder]="'Add email'"
[secondaryPlaceholder]="'Enter email address(es)'"
[clearOnBlur]="true"
[addOnPaste]="true"
[addOnBlur]="true"
[pasteSplitPattern]="splitPattern"
theme='bootstrap'
required >
</tag-input>
对于 2),我尝试在验证器中将“return null”更改为 control.value ......但这没有用