1
我正在嘗試簡單的angular
日期驗證當選擇日期超過當前日期時,todate
在提交時給出錯誤消息。但產出並不如預期。它在選擇任何日期時給出錯誤,但是在調試斷點中正確檢查條件。
plunker鏈接「: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>
什麼是你的模型?創建一個[plunker](https://plnkr.co/)/ [jsfiddle](https://jsfiddle.net/)等例子,請 – GProst