angular - 在 Angular 4 上验证电子邮件

标签 angular angular4-forms

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

app.component.html app.component.ts modal

最佳答案

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

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();
            });
    }
}

关于angular - 在 Angular 4 上验证电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46902766/

相关文章:

android - Ionic Live Reload Capacitor Gradle 构建 - 失败!找不到 Java 运行时

javascript - 如何在 Angular 2 中初始化外部对象?

angular - 响应式(Reactive)表单自定义验证器不显示 Angular6 中的错误

angular - 我正在我的功能模块中导入 FormGroup,它在 Angular4 多模块结构中引发错误

javascript - 单选按钮拦截 Angular/Javascript 中未经检查的事件

Angular Schematics - 添加库到 NgModule 导入

angular - 如何使用 EmbeddedViewRef 的上下文变量

javascript - 切换用户可以删除文本框中的文本与不删除文本的状态

带有 ng 值访问器的 Angular Material 2 自定义组件

html - Bootstrap 输入 css 问题