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

时间:2018-11-09 10:52:49

标签: javascript angularjs angularjs-directive angularjs-bootstrap

我有一个表单,其中有两个字段DOBmarriage date。如果结婚日期为less than DOB,我想显示red border。如果DOB is greater than marriage我可以得到提示,但我也不能显示红色边框,如果我的字段无效,那么我希望表格应该无效也是

这是我的代码 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']);
            }
        });
    }
});

如果结婚日期大于我要显示的红色边框。我的表格应该无效

 <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>

2 个答案:

答案 0 :(得分:0)

您可以使用:ng-class

类似的东西:

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

您可以将其添加到输入中。

然后将.verified添加到您的CSS中:

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

这是plnkr

答案 1 :(得分:0)

根据输入字段的有效性进行申请非常简单。添加CSS规则,它将在字段无效时触发。

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