Javascript 如何在angular中验证日期格式
您好,我只是想知道这个问题,例如,如果我有angular UI date piker date格式yyyy-MM-dd。如果用户通过键入输入日期并输入错误的格式(例如YY-dd-MM),应用程序会认为它是有效日期并将其保存为yyyy-MM-dd。我如何验证格式是否正确Javascript 如何在angular中验证日期格式,javascript,angularjs,validation,date,datetime,Javascript,Angularjs,Validation,Date,Datetime,您好,我只是想知道这个问题,例如,如果我有angular UI date piker date格式yyyy-MM-dd。如果用户通过键入输入日期并输入错误的格式(例如YY-dd-MM),应用程序会认为它是有效日期并将其保存为yyyy-MM-dd。我如何验证格式是否正确 <div class="panel panel-default"> <div class="panel-heading">Date </div> <div class="p
<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>
使用本机的static
Date.parse()
方法,或者看看这个库,它有很好的日期抽象
您还可以使用正则表达式检查日期,例如
/\d{4}-\d{2}-\d{2}/.test($scope.model.date)
但是它应该与Date.parse成对出现,因为9999-99-99将通过这种简单的验证器
无论如何,这里有一些用于日期验证的方法。使用本机static
date.parse()
方法,或者看看库,它有很好的日期抽象
您还可以使用正则表达式检查日期,例如
/\d{4}-\d{2}-\d{2}/.test($scope.model.date)
但是它应该与Date.parse成对出现,因为9999-99-99将通过这种简单的验证器
无论如何,这里有一些日期验证的例子。我更新了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
我更新了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
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)
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)
“在ES5之前,不建议使用Date.parse,因为字符串的解析完全依赖于实现”-@java-addict301来吧,你将在哪一年离开?还有es2019,我只是一个信使——如果你不同意的话,就和Mozilla谈谈。它指出“在不同主机解析日期字符串的方式上仍然存在许多差异,因此应该手动解析日期字符串(如果要容纳许多不同的格式,库可以提供帮助)。”“不建议使用date.parse,因为直到ES5,字符串的解析完全依赖于实现”-@java-addict301 c'mon,你打算在哪一年离开?还有es2019,我只是一个信使——如果你不同意的话,就和Mozilla谈谈。它指出“在不同主机解析日期字符串的方式上仍然存在许多差异,因此应该手动解析日期字符串(如果要容纳许多不同的格式,库可以提供帮助)。”