我正在尝试一个简单的angular
日期验证,其中todate
在选择日期超过当前日期时会在提交时给出错误消息。但输出并不像预期的那样。选择任何日期时出错,但条件是在调试断点中正确检查。
plunker link":plunker
以下是代码:
$scope.form1submit=function(date){
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
todayDate = mm+'/'+dd+'/'+yyyy;
console.log(todayDate);
var toDate = date.toDate;
var toDatelength = date.toDate.length;
if(toDate > todayDate && toDatelength > 0){
$scope.form1.todate.$setValidity("todateerror", false);
}else{
$scope.form1.todate.$setValidity("todateerror", true);
}
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://www.jqueryscript.net/demo/Simple-Date-Picker-For-jQuery-AngularJS-Datepicker/"></script>
<span class="">
<label class="">to date</label>
<input id="toDate" name="todate" type="text" class=""
ng-model-options="{allowInvalid: true}"
placeholder="To Date"
data-datepicker="{theme: 'flat'}"
ng-model="date.toDate"
ng-disabled="date.fromdate"
ng-required="date.fromdate" />
<span ng-show="form1.$submitted && form1.todate.$error">
<div ng-message="todateerror" class="">To date is more than todays date</div>
</span>
</span>
答案 0 :(得分:0)
Here我修复了你的代码。
form1.todate.$error
您必须使用form1.todate.$error.todateerror