我对 Angularjs 完全陌生,并尝试验证 2 个场景。我有 2 个文本框,一个包含开始日期,另一个包含结束日期。我正在查
- 如果开始日期不大于或等于今天,则在 UI 上显示验证错误。应该是今天或今天之后的任何一天。
- 如果开始日期大于结束日期,则在 UI 上显示验证错误。结束日期应大于开始日期。
我尝试了下面的代码,但没有成功。有什么建议请。
网页代码
<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;
}
};
- 我将两个日期控件的输入类型作为文本。我正在使用引导日期选择器。
您的第一位逻辑相反,您必须从 startDate 构造一个新日期来与今天的日期进行比较。您还将 curDate 设置为范围,$scope.curDate = new Date()
但后来你将其引用为curDate
没有$scope
所以它是未定义的。最后,你需要投射stateDate
and 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;
}
};
工作示例:http://jsfiddle.net/peceLm14/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)