【问题标题】:validate email on angular 4在 Angular 4 上验证电子邮件
【发布时间】:2017-10-24 05:22:58
【问题描述】:

我是 Angular 4 中反应形式的新手。 我如何在电子邮件中放置模式验证? 我对https://angular.io/api/forms/EmailValidator 感到困惑

app.component.htmlapp.component.tsmodal

【问题讨论】:

    标签: angular angular4-forms


    【解决方案1】:

    如果您需要邮件验证器,可以使用我的代码:

    export class GlobalValidator
    {
        static mailFormat(control: FormControl)
        {
            const EMAIL_REGEXP = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    
            if (control.value && !EMAIL_REGEXP.test(control.value))
            {
                return {
                    validateEmail: {
                        valid: false,
                        message: "Not valid email."
                    }
                };
            }
    
            return null;
        }
    
        //other validators like numeric, lowerCase, uppercase, conditional, compare ...
    }
    

    当设置形成时:

    this.userForm = new FormGroup({
                    email: new FormControl(null, [Validators.required, GlobalValidator.mailFormat]),
                    displayName: new FormControl(null),
                    type: new FormControl(null, [Validators.required]),
                    password: new FormControl(null, [Validators.required, Validators.minLength(6), GlobalValidator.numericRule, GlobalValidator.lowerCaseRule, GlobalValidator.upperCaseRule, GlobalValidator.nonAlpahuNericCaseRule]),
                    passwordConfirm: new FormControl(null, [Validators.required]),
                }, GlobalValidator.compareValidator("password", "passwordConfirm"));
    

    这里是完整的全局验证器:

    export class GlobalValidator
    {
        static mailFormat(control: FormControl)
        {
            const EMAIL_REGEXP = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    
            if (control.value && !EMAIL_REGEXP.test(control.value))
            {
                return {
                    validateEmail: {
                        valid: false,
                        message: "Not valid email."
                    }
                };
            }
    
            return null;
        }
    
        static numericRule(control: FormControl)
        {
            if (control.value && !(/\d/.test(control.value)))
            {
                return {
                    numericRule: {
                        valid: false,
                        message: "Numeric char missing."
                    }
                };
            }
            return null;
        }
    
        static lowerCaseRule(control: FormControl)
        {
            if (control.value && !(/[a-z]/.test(control.value)))
            {
                return {
                    lowerCaseRule: {
                        valid: false,
                        message: "Lower case character missing."
                    }
                };
            }
            return null;
        }
    
        static upperCaseRule(control: FormControl)
        {
            if (control.value && !(/[A-Z]/.test(control.value)))
            {
                return {
                    upperCaseRule: {
                        valid: false,
                        message: "Upper case character missing."
                    }
                };
            }
            return null;
        }
    
        static nonAlpahuNericCaseRule(control: FormControl)
        {
            if (control.value && !(/[\W_]+/g.test(control.value)))
            {
                return {
                    nonAlpahuNericCaseRule: {
                        valid: false,
                        message: "Non-alphanumeric character missing."
                    }
                };
            }
            return null;
        }
    
        static compareValidator(fc1: string, fc2: string)
        {
            return (group: FormGroup): { [key: string]: any } =>
            {
                if (group.value)
                {
                    const password = group.value[fc1];
                    const passwordConfirm = group.value[fc2];
                    if (password !== passwordConfirm)
                    {
                        return {
                            compareFailed: {
                                valid: false,
                                message: "Password not match."
                            }
                        }
                    }
                }
                return null;
            }
        }
    
        static conditional(conditional: (group: FormGroup) => boolean, validator)
        {
            return function (control)
            {
                revalidateOnChanges(control);
    
                if (control && control._parent)
                {
                    if (conditional(control._parent))
                    {
                        return validator(control);
                    }
                }
            };
        }
    }
    
    function revalidateOnChanges(control): void
    {
        if (control && control._parent && !control._revalidateOnChanges)
        {
            control._revalidateOnChanges = true;
            control._parent
                .valueChanges
                .distinctUntilChanged((a, b) =>
                {
                    // These will always be plain objects coming from the form, do a simple comparison
                    if (a && !b || !a && b)
                    {
                        return false;
                    } else if (a && b && Object.keys(a).length !== Object.keys(b).length)
                    {
                        return false;
                    } else if (a && b)
                    {
                        for (let i in a)
                        {
                            if (a[i] !== b[i])
                            {
                                return false;
                            }
                        }
                    }
                    return true;
                })
                .subscribe(() =>
                {
                    control.updateValueAndValidity();
                });
        }
    }
    

    【讨论】:

    【解决方案2】:

    这三个选项中的任何一个都将为您验证电子邮件,而无需提供正则表达式,因为 Angular 已经为您完成了

    <input type="email" name="email" ngModel email>
    <input type="email" name="email" ngModel email="true">
    <input type="email" name="email" ngModel [email]="true">
    

    https://angular.io/api/forms/EmailValidator

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-17
      • 2017-10-03
      • 2020-06-18
      • 1970-01-01
      • 2019-07-02
      • 2018-08-17
      • 2023-03-26
      • 2022-12-11
      相关资源
      最近更新 更多