简体   繁体   English

AngularJS指令模糊事件未设置验证

[英]AngularJS directive blur event not setting validation

I am trying to make a custom AngularJS directive for the first time where a few currency validation checks are done and the form validity is set. 我正在尝试做一个自定义AngularJS指令的第一次,其中完成了一些货币验证检查并设置了表单有效性。 I am trying to set up a blur event using link that validates the user input and then adds CSS and html elements if any of the validation checks fail. 我正在尝试使用链接来设置模糊事件,该链接用于验证用户输入,然后在任何验证检查失败的情况下添加CSS和html元素。 Here is what I have so far: 这是我到目前为止的内容:

angular.module('sharedDirectives', []).directive('abccurrency', function () {
return {
    restrict: "A",
    require: 'ngModel',
    replace: true,
    link: function (scope, element, attr, ctrl) {
        var commaRegEx = /^(?:\d+(?:,\d{3})*(?:\.\d{2})?)$/;
        var min = 0;
        var max = 99999.99;

        element.bind('blur', function (value) {

            var valueNoComma = parseFloat(value.currentTarget.value.replace(/,/g, ''));
            ctrl.$setViewValue(String(value.currentTarget.value).replace(/,/g, ''));

            var validCommas = commaRegEx.test(value.currentTarget.value);
            ctrl.$setValidity('commaValidate', validCommas);

            if (validCommas === true) {
                var aboveMin = min < valueNoComma;
                ctrl.$setValidity('minValidate', aboveMin);
            }

            if (validCommas === true) {
                var belowMax = valueNoComma <= max;
                ctrl.$setValidity('maxValidate', belowMax);
            }
        });
    }
};
});

Here is an example of the html I am trying to apply this directive to: 这是我尝试将此指令应用于的html的示例:

<form name="testform">
<div class="row top-buffer15">
    <div class="col-md-3" ng-class="{'has-error' : testform.currencytest.$invalid && testform.currencytest.$touched}">
        <input type="text" ng-model="matchpay.test" id="currencytest" name="currencytest" class="form-control" abccurrency>
        <p style="color: red" ng-show="testform.currencytest.$error.commaValidate">Currency fields must the form XX,XXX.XX or XXXXX.XX.</p>
        <p style="color: red" ng-show="testform.currencytest.$error.minValidate">Currency fields must be more than zero.</p>
        <p style="color: red" ng-show="testform.currencytest.$error.maxValidate">Currency fields cannot be more than $99,999.99.</p>
    </div>
</div>
</form>

Now my problem is that in one area of the project, this works just as expected. 现在我的问题是,在项目的一个区域中,该项目按预期工作。 The user enters their input and the CSS styling and error notification html is displayed. 用户输入他们的输入,并显示CSS样式和错误通知html。 In another area, though, the blur event doesn't actually change anything, even though the code in the directive is ran. 但是,在另一个区域中,即使伪指令中的代码已运行,blur事件实际上也不会更改任何内容。 Only after you click in and out of the input, is the correct validation information displayed. 仅在您单击输入和退出输入之后,才会显示正确的验证信息。 Is there something immediately obvious that I am doing wrong? 是否立即发现我做错了什么? It is unclear to me why it would work in one place but not another. 我不清楚为什么它会在一个地方而不是另一个地方工作。 Please let me know if you need additional information, I'd be happy to post screenshots of the difference in functionality. 如果您需要其他信息,请告诉我,我们很乐意发布功能差异的屏幕截图。

After doing some research and confirming this should work. 经过一些研究并确认这应该起作用。 I tried putting the $setViewValue call at the end to see if that would do anything. 我尝试将$ setViewValue调用放在末尾,以查看是否可以执行任何操作。 As it turns out, it indeed prevent the $setValidity calls for whatever reason. 事实证明,无论出于何种原因,它的确阻止了$ setValidity调用。 Any ideas as to why? 有什么想法吗? This refactor allows the directive to correctly notify the user of incorrect input. 此重构使指令可以正确通知用户错误的输入。

element.bind('blur', function (value) {

            var valueNoComma = parseFloat(value.currentTarget.value.replace(/,/g, ''));

            var validCommas = commaRegEx.test(value.currentTarget.value);
            var aboveMin = min < valueNoComma;
            var belowMax = valueNoComma <= max;

            ctrl.$setValidity('commaValidate', validCommas);
            ctrl.$setValidity('minValidate', aboveMin);
            ctrl.$setValidity('maxValidate', belowMax);
            ctrl.$setViewValue(String(value.currentTarget.value).replace(/,/g, ''));


        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM