简体   繁体   English

如果日期在Angular JS中无效,如何使边框变为红色?

[英]how to make border red if date is invalid in angular js?

I have a form in in which I have two field DOB , and marriage date .I want if marriage date is less than DOB I want to show red border .I am able to get if DOB is greater than marriage But I am not able to show red border as well I want form should be invalid if my fields are invalid then my form is also invalid 我有一个表格,其中有两个字段DOBmarriage日期。如果结婚日期less than DOB我想显示red边框。如果DOB is greater than marriage我可以得到,但我不能显示红色的边框,以及我想要的形式应该是,如果我的领域是无效的无效 ,那么我的状态也无效

here is my code http://plnkr.co/edit/neixp9ZARRAQ33gKSV9u?p=preview 这是我的代码http://plnkr.co/edit/neixp9ZARRAQ33gKSV9u?p=preview

 $scope.changedate =function(obj){

    console.log(obj.name)
    if(obj && obj.name){
            obj.longDate = moment(obj.name).format('x');
        }

        if(parseInt($scope.c['marriage date'].longDate) > parseInt($scope.c['date of birth'].longDate)){
          alert('not bigger')
        }
  }




app.directive('viewValueChanged', function viewValueChangedDirective() {
    return {
        restrict: "A",
        require: 'ngModel',
        link: linkFn
    };

    function linkFn(scope, elem, attrs, ngModel) {
        scope.$watch(function () {
            return ngModel.$viewValue;
        }, function (newValue, oldValue) {
            if (newValue && newValue !== oldValue) {
                scope.$parent.$eval(attrs['viewValueChanged']);
            }
            // in case of user entered invalid value
            if(newValue === null) {
                scope.$parent.$eval(attrs['viewValueChanged']);
            }
        });
    }
});

if marriage date is bigger than I want to show red border as well .my form should be invalid 如果结婚日期大于我要显示的红色边框,则我的表格应该无效

 <form name="userForm">
    <li ng-repeat="(key, x) in c">
       <p class="input-group"  ng-if="x.date=='date'">{{key}}
          <input name="{{key}}" type="text" view-value-changed='changedate(x)' class="form-control" uib-datepicker-popup="{{format}}" ng-model="x.name" is-open="x.opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="formats" />
                <span id="" ng-if="userForm[key].$invalid" class="help-block" style="margin-bottom:0">Invalid date </span>

          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open1(x)"><i class="glyphicon glyphicon-calendar"></i></button>
          </span>
        </p>

         <p class="input-group"  ng-if="x.date=='date2'">{{key}}
          <input  name="{{key}}"type="text" view-value-changed='changedate(x)' class="form-control" uib-datepicker-popup="{{format}}" ng-model="x.name" is-open="x.opened" datepicker-options="dateOptions2" ng-required="true" close-text="Close" alt-input-formats="formats" />
            <span id="" ng-if="userForm[key].$invalid" class="help-block" style="margin-bottom:0">Invalid date </span>

          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open1(x)"><i class="glyphicon glyphicon-calendar"></i></button>
          </span>

        </p>
    </li>
    </form>

You can use: ng-class 您可以使用: ng-class

Something like: 就像是:

 ng-class="{'verified': userForm[key].$invalid}"

You can add it to your input. 您可以将其添加到输入中。

Then add .verified to your css: 然后将.verified添加到您的CSS中:

input.form-control.verified {
   border: 1px solid red;
}

This is the plnkr 这是plnkr

It would be very simple to apply based on the validity on input field. 根据输入字段的有效性进行应用非常简单。 Add CSS rule and it will trigger when field becomes invalid. 添加CSS规则,它将在字段变为无效时触发。

inupt[uib-datepicker-popup].ng-invalid {
   border: 1px solid red;
}

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

相关问题 如何在Angular JS中的按钮的输入字段中显示红色边框 - how to show red border in input field on button In angular js Validation in JavaScript - 如何让红色边框消失? - Validation in JavaScript - how to make the red border disappear? 使用角度js验证问题(firefox上的红色边框显示)? - validation issue using angular js (red border display on firefox)? 如何通过创建边框红色来突出显示无效的jQuery输入 - how to highlight invalid jquery input by creating border red 如何在警报Dismis上使输入字段边框变为红色 - How to make input field border red on alert dismis 我如何使用angular js在错误或无效的输入字段的边框颜色? - how can i can the border-color of input field on error or invalid using angular js ?? 如果总计无效,请将字段文本和边框设置为红色 - If total is invalid, set the field text and border to red 如何修复JS中的无效日期? - How fix Invalid date in JS? 如何仅在第一次单击 MUI React 后调用无效的红色边框样式? - How to invoke invalid red border styling only after first click in MUI React? 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色? - How to show particular textbox border red color if i enter invalid email or blank in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM