javascript 如何以角度验证日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30218987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to validate date format in angular
提问by Anton Savchenko
Hi just I was wondering about problem for example if I have angular UI date piker date format yyyy-MM-dd .what if the users enter date by typing and they enter wrong format for example yyyy-dd-mm the app think it is valid date and save it as YYYY-MM-dd. just how can I validate format it is correct ?
嗨,我只是想知道问题,例如,如果我有 angular UI date piker 日期格式 yyyy-MM-dd 。如果用户通过键入输入日期并且他们输入错误的格式,例如 yyyy-dd-mm 应用程序认为它是有效的怎么办日期并将其保存为 YYYY-MM-dd。我该如何验证格式是否正确?
<div class="panel panel-default">
<div class="panel-heading">Date </div>
<div class="panel-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="control-label"><i class="fa fa-calendar"></i><i class="icon-required"></i>Date [YYYY-MM-DD]</label>
<div class="input-group">
<input type="text" class="form-control" datepicker-popup="yyyy-MM-dd" data-ng-model="model.date" is-open="isDatePickerOpen" close-text="Close" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" data-ng-click="openDatePicker($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</div>
<div class="validation-warning" data-ng-show="displayModel.showDateValidator"><i class="icon-alert"></i>Required</div>
</div>
</div>
</div>
</div>
</div>
validator
验证器
var formValidator = function ($scope) {
var isDateValid = function () {
return $scope.model != null && $scope.model.date != null && $scope.model.date !== '';
};
return {
valid: function () {
var isValid = true;
if (!isDateValid()) {
isValid = false;
}
return isValid;
},
addWatches: function () {
$scope.$watch('model.date', function () {
$scope.displayModel.showDateValidator = !isDateValid();
});
}
};
};//ActivitiesFormValidator
采纳答案by Anton Savchenko
I update your FormValidator function to use native JavaScript
我更新您的 FormValidator 函数以使用原生 JavaScript
var formValidator = function ($scope) {
var isDateValid = function () {
//return $scope.model != null && $scope.model.date != null && $scope.model.date !== '';
var dateTime = $scope.model.date;
if (dateTime === null) return false;
var day = dateTime.getDate();
var month = dateTime.getMonth() + 1;
var year = dateTime.getFullYear();
var composedDate = new Date(year, month, day);
return composedDate.getDate() === day &&
composedDate.getMonth() === month &&
composedDate.getFullYear() === year;
};
return {
valid: function () {
var isValid = true;
if (!isDateValid()) {
isValid = false;
}
return isValid;
},
addWatches: function () {
$scope.$watch('model.date', function () {
$scope.displayModel.showDateValidator = !isDateValid();
});
}
};
};//ActivitiesFormValidator
回答by Ajith Emalathas
Momentjs library might be useful for you.
Momentjs 库可能对您有用。
moment("2010 13", "YYYY MM").isValid(); // false (not a real month)
moment("2010 11 31", "YYYY MM DD").isValid(); // false (not a real day)
moment("2010 2 29", "YYYY MM DD").isValid(); // false (not a leap year)
moment("2010 notamonth 29", "YYYY MMM DD").isValid(); // false (not a real month name)
回答by Anton Savchenko
Use native static Date.parse()
method or take a look at momentjslibrary, which has great date abstraction over it.
使用原生静态Date.parse()
方法或查看momentjs库,它具有很好的日期抽象。
Also you can use regular expression to check your date, e.g.
您也可以使用正则表达式来检查您的日期,例如
/\d{4}-\d{2}-\d{2}/.test($scope.model.date)
but it should come in the pair with Date.parse, couse 9999-99-99 will pass through such simple validator.
但它应该与 Date.parse 成对出现,因为 9999-99-99 将通过这样简单的验证器。
Anyway, here some regexpsfor date validation to look at.
无论如何,这里有一些用于日期验证的正则表达式。