Angularjs initial form validation with directives

前端 未结 3 2222
春和景丽
春和景丽 2021-02-14 09:49

I have a validation directive called valid-number that is used to set the validity of a form using $setValidity - this works fine for any text values that I type in

3条回答
  •  既然无缘
    2021-02-14 10:06

    You can simply call your verification function during the linking phase, like in this fiddle :

    link: function(scope, elm, attrs, ctrl) {                       
        var regex=/\d/;
        var verificationFunction = function(viewValue) {
            var floatValue = parseFloat(viewValue);
    
            if(regex.test(viewValue)) {
                ctrl.$setValidity('validNumber',true);
                return viewValue;
            }
            else {
                ctrl.$setValidity('validNumber',false);
                return undefined;
            }
        };
    
        ctrl.$parsers.unshift(verificationFunction);
        verificationFunction();
    }
    

提交回复
热议问题