javascript - validate mulitiple date fields -
i'm creating form have datepicker , timepicker both starting date , ending date. in total got 4 fields. able validate if startdate (a combination of startdate , starttime) earliere enddate. can't seem figure out , how should put validation.
so whenever 1 of fields edited should trigger validation.
i can't seem figure out , how create validation, i've tried create custom validation named method-validation retrieve method controller decided if validation propper, can't put on 1 field, because if change other field validation not triggered.
anyone have idear working ?
bottom line is, want able take 2 , 2 fields set them togehter, , compare them, , show message if end smaller start.
follow up:
the issue isn't comparing 2 dates. can achieve.
<input type="text" ng-model="activity.startdate" date-picker> <input type="text" ng-model="activity.starttime" time-picker> <input type="text" ng-model="activity.enddate" date-picker> <input type="text" ng-model="activity.endtime" time-picker>
it making sure combination of 2 field isn't off
module.directive('validdate', function(){ return { require: 'ngmodel', link: function(scope, elm, attrs, ctrl) { function validator(value){ var valid = true; if (value != undefined) { value = moment(value, 'yyyy-mm-dd').format('yyyy-mm-dd'); if (((attrs.validdate == 'start') && (value >= scope.$eval(attrs.otherdate))) || ((attrs.validdate == 'end') && ((value < scope.$eval(attrs.otherdate))))) { valid = false; } } else { valid = false; } ctrl.$setvalidity('date-valid', valid); return value; } scope.$watch([attrs.otherdate, attrs.ngmodel] ,function(newvalue) { validator(scope.$eval(attrs.ngmodel)); }, true); } }; });
usage:
<input type="date" ng-model="startdate" valid-date="start" other-date="enddate" /> <input type="date" ng-model="enddate" valid-date="end" other-date="startdate" />
let me know if works. used momentjs library formatting here feel free change it.
Comments
Post a Comment