我完全是Angularjs的新手,并试图验证2个场景。我有2个文本框,一个带有开始日期,另一个带有结束日期。我正在检查
我尝试了以下不起作用的代码。请提出任何建议。
Html代码
<label for="endDate" class="control-label">Start Date:</label>
<div>
<input type="text" class="form-control"
id="startDate" ng-model="startDate" />
</div>
<label for="text" class="control-label">End Date:</label>
<div>
<input type="text" class="form-control"
id="endDate" ng-model="endDate"
ng-change='checkErr(startDate,endDate)' />
</div>
<span>{{errMessage}}</span>
js code
$scope.checkErr = function(startDate,endDate){
$scope.errMessage = '';
$scope.curDate = new Date();
if(startDate < endDate){
$scope.errMessage = 'End Date should be greate than start date';
return false;
}
if(startDate < curDate){
$scope.errMessage = 'Start date should not be before today.';
return false;
}
};
答案 0 :(得分:8)
你在第一位有相反的逻辑,你必须从startDate构建一个新的日期,以便与今天的日期进行比较。您还可以将curDate设置为范围$scope.curDate = new Date()
,但之后您将其引用为curDate
而没有$scope
,因此未定义。最后,您还需要将stateDate
和endDate
投射到某个日期。否则你只是在比较字符串。
$scope.checkErr = function(startDate,endDate) {
$scope.errMessage = '';
var curDate = new Date();
if(new Date(startDate) > new Date(endDate)){
$scope.errMessage = 'End Date should be greater than start date';
return false;
}
if(new Date(startDate) < curDate){
$scope.errMessage = 'Start date should not be before today.';
return false;
}
};
答案 1 :(得分:1)
看起来您正在引用未定义的curDate
。将条件更改为if (startDate < $scope.curDate)
。请参阅工具示例的小提琴http://jsfiddle.net/4ec3atzk/1/
$scope.checkErr = function(startDate,endDate){
$scope.errMessage = '';
$scope.curDate = new Date();
if (startDate < endDate){
$scope.errMessage = 'End Date should be greate than start date';
return false;
}
if (new Date(startDate) < $scope.curDate){
$scope.errMessage = 'Start date should not be before today.';
return false;
}
};
答案 2 :(得分:1)
检查此link并明确说明
答案 3 :(得分:0)
$scope.datepickerObjectfromdates = {
todayLabel: 'Today',
closeLabel: 'Close',
setLabel: 'Ok',
setButtonType : 'button-calm',
todayButtonType : 'button-calm',
closeButtonType : 'button-calm',
inputDate: new Date(),
mondayFirst: true,
templateType: 'popup',
showTodayButton: 'true',
modalHeaderColor: 'bar-calm',
modalFooterColor: 'bar-calm',
callback: function (val) {
var getdate = GetFormattedFromDates(val);
$scope.date.FromDates = getdate;
localStorage.date = $scope.FromDates;
},
dateFormat: 'MM-dd-yyyy', //Optional
closeOnSelect: false, //Optional
};
function GetFormattedFromDates(val) {
if(typeof(val)==='undefined')
{
$scope.date.FromDates = '';
}
else {
var todayTime = new Date(val);
var month = todayTime.getMonth() + 1;
var day = todayTime.getDate();
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
var year = todayTime.getFullYear();
return day + "/" + month + "/" + year;
}
}
$scope.datepickerObjecttodates = {
todayLabel: 'Today',
closeLabel: 'Close',
setLabel: 'Ok',
setButtonType : 'button-calm',
todayButtonType : 'button-calm',
closeButtonType : 'button-calm',
inputDate: new Date(),
mondayFirst: true,
templateType: 'popup',
allowOldDates: false,
showTodayButton: 'true',
modalHeaderColor: 'bar-calm',
modalFooterColor: 'bar-calm',
callback: function (val) {
var getdate = GetFormattedToDates(val);
$scope.date.ToDates = getdate;
//$scope.date.ToDates = getdate.clear();
},
dateFormat: 'dd-MM-yyyy', //Optional
closeOnSelect: false, //Optional
};
function GetFormattedToDates(val) {
if (typeof(val) === 'undefined') {
$scope.ToDates = '';
}
else {
var todayTime = new Date(val);
var month = todayTime.getMonth() + 1;
var day = todayTime.getDate();
if (day < 10) {
day = '0' + day;
}
if (month < 10) {
month = '0' + month;
}
var year = todayTime.getFullYear();
return day + "/" + month + "/" + year;
}
}