Form Validation - Email Validation not working as expected in AngularJs

后端 未结 6 683
北荒
北荒 2020-12-25 12:35

I am using Angular for form validation.

Here is what I use - plunker-edit I have taken this code from Angularjs documentation - Binding to form and control state Hav

6条回答
  •  有刺的猬
    2020-12-25 13:07

    As ReCaptcha suggested I ended up creating a custom validation directive

    var app = angular.module('login-form', []);
    var INTEGER_REGEXP = new RegExp('^[a-z0-9]+(\.[_a-z0-9]+)*@@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,50})$', 'i');
    app.directive('cemail', function () {
        return {
            require: 'ngModel',
            link: function (scope, elm, attrs, ctrl) {
                ctrl.$parsers.unshift(function (viewValue) {
                    if (INTEGER_REGEXP.test(viewValue)) {
                        // it is valid
                        ctrl.$setValidity('cemail', true);
                        return viewValue;
                    } else {
                        // it is invalid, return undefined (no model update)
                        ctrl.$setValidity('cemail', false);
                        return undefined;
                    }
                });
            }
        };
    });
    

    and in html

    
    
    
        Required
        Invalid Email
    
    

提交回复
热议问题