我有三个名为"Day"
,"Month"
和"Year"
的下拉字段。我找到了很多方法来检查那些下拉列表中的选定值是angularjs中的有效日期。
以下是填充到三个下拉字段中的数据。
var numberOfYears = (new Date()).getYear() - 10;
var years = $.map($(Array(numberOfYears)), function (val, i) { return i + 1900; });
var months = $.map($(Array(12)), function (val, i) { return i + 1; });
var days = $.map($(Array(31)), function (val, i) { return i + 1; });
$scope.Years = years;
$scope.Days = days;
$scope.Months = months;
请告诉我如何在AngularJS中检查日期是否有效
答案 0 :(得分:1)
在onchange事件的每个下拉列表中,您可以检查日期是否有效
function ValidateDate() {
if (!isDate($('#dateid').val() + '/' + $('#monthid').val() + '/' + $('#yearid').val())) {
alert('Date is not valid');
}
}
//format(dd/MM/yyyy)
function isDate(value) {
var re = /^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-.\/])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
var flag = re.test(value);
return flag;
}
有关支票有效日期的更多信息,请按照validate date in JQuery
进行操作答案 1 :(得分:1)
我不知道天气对你有帮助。但我刚尝试了
var myApp = angular.module('myApp', []);
myApp.controller("MyCtrl", MyCtrl);
function MyCtrl($scope, $filter) {
$scope.y = "1991";
$scope.m = "2";
$scope.d = "25";
$scope.$watch('[y,m,d]', function() {
var year = new Date($scope.m + "-" + $scope.d + "-" + $scope.y);
if (year == "Invalid Date") {
$scope.flag = false;
} else {
$scope.flag = true;
}
}, true);
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<form>
<select ng-model="y">
<option val="1991">1991</option>
<option val="1992">1992</option>
<option val="c">c</option>
</select>
<select ng-model="m">
<option>1</option>
<option>2</option>
<option>a</option>
</select>
<select ng-model="d">
<option>25</option>
<option>26</option>
<option>b</option>
</select>
</form>
<span>Valid : {{flag}}</span>
</div>