使用角度2最新版本中的模型驱动表单发送电子邮件验证。请给出最新版本的角度2示例。
答案 0 :(得分:3)
验证Data Driven电子邮件的最简单方法(在TS文件中定义):
'email': ['',[Validators.required,Validators.pattern("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")]],
对于模板驱动(在HTML文件中定义):
<div class="form-group">
<label for="email"> Email</label>
<input type="text"
id="email"
class="form-control"
ngModel
name="email"
required
pattern=" [a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"
></div>
答案 1 :(得分:1)
来自Cory Rylan博客文章:https://coryrylan.com/blog/angular-2-form-builder-and-validation-management
验证服务:
app.component.ts
错误组件:
//Original version created by Cory Rylan: https://coryrylan.com/blog/angular-2-form-builder-and-validation-management
import { IsValidDate } from '../helpers/date.helper'
export class ValidationService {
static getValidatorErrorMessage(validatorName: string, validatorValue?: any) {
let config = {
'required': 'Required',
'invalidNumberField': 'Only numbers allowed',
'invalidDateField': 'Not a valid date',
'invalidCreditCard': 'Is invalid credit card number',
'invalidEmailAddress': 'Invalid email address',
'invalidPassword': 'Invalid password. Password must be at least 6 characters long, and contain a number.',
'invalidPasswords': 'Invalid passwords. Passwords must match.',
'minlength': `Minimum length ${validatorValue.requiredLength}`
};
// console.log(" config = " + JSON.stringify(config));
// console.log(" validator name: " + validatorName);
// console.log(" config = req " + JSON.stringify(config["required"]));
// console.log(" config = nan " + JSON.stringify(config["invalidNumberField"]));
return config[validatorName];
}
static numberFieldValidator(control) {
// if (control.value.match(/^([0-9]|[0-9][0-9]|[1-9][0-9][0-9])$/)) {
// return null;
// } else {
// return { 'invalidNumberField': true };
// }
return null;
}
static dateFieldValidator(control) {
var e: boolean;
if (IsValidDate(control.value)) {
return null;
} else {
return { 'invalidDateField': true };
}
}
static creditCardValidator(control) {
// Visa, MasterCard, American Express, Diners Club, Discover, JCB
if (control.value.match(/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$/)) {
return null;
} else {
return { 'invalidCreditCard': true };
}
}
static emailValidator(control) {
// RFC 2822 compliant regex
if (control.value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)) {
return null;
} else {
return { 'invalidEmailAddress': true };
}
}
static passwordValidator(control) {
// {6,100} - Assert password is between 6 and 100 characters
// (?=.*[0-9]) - Assert a string has at least one number
if (control.value.match(/^(?=.*[0-9])[a-zA-Z0-9!@#$%^&*]{6,100}$/)) {
return null;
} else {
return { 'invalidPassword': true };
}
}
static passwordCompareValidator(fg) {
if (fg.value.password === fg.value.confirmPassword) {
return null;
} else {
return { 'invalidPasswords': true };
}
}
}
Html用法:
import { Component, Input } from '@angular/core';
import { FormControl } from '@angular/forms';
import { ValidationService } from '../services/validation.service';
@Component({
selector: 'kg-errorMessages',
template: `<div *ngIf="errorMessage !== null">{{errorMessage}}</div>`
})
export class ErrorMessagesComponent {
@Input() control: FormControl;
@Input() name: string;
constructor() { }
get errorMessage() {
for (let propertyName in this.control.errors) {
if (this.control.errors.hasOwnProperty(propertyName) && this.control.touched) {
return ValidationService.getValidatorErrorMessage(propertyName, this.control.errors[propertyName]);
}
}
return null;
}
}