admin管理员组文章数量:1341867
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 ?
<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
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 ?
<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
Share
edited May 13, 2015 at 16:20
asked May 13, 2015 at 15:16
user4681575user4681575
3 Answers
Reset to default 4Momentjs library might be useful for you.
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)
Use native static Date.parse()
method or take a look at momentjs library, which has great date abstraction over it.
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 e in the pair with Date.parse, couse 9999-99-99 will pass through such simple validator.
Anyway, here some regexps for date validation to look at.
I update your FormValidator function to use native 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 posedDate = new Date(year, month, day);
return posedDate.getDate() === day &&
posedDate.getMonth() === month &&
posedDate.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
本文标签: javascripthow to validate date format in angularStack Overflow
版权声明:本文标题:javascript - how to validate date format in angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743686440a2521974.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论