Skip to content

Instantly share code, notes, and snippets.

@leebrandt
Created May 12, 2016 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leebrandt/8c95ad5f80e383ff480921fb8b0632c6 to your computer and use it in GitHub Desktop.
Save leebrandt/8c95ad5f80e383ff480921fb8b0632c6 to your computer and use it in GitHub Desktop.
(function () {
var timePicker = function () {
return {
restrict: 'E',
replace: true,
require: 'ngModel',
scope:{},
template: '<input type="text" />',
link: function (scope, elem, attrs, ctrl) {
elem.on('blur', function () {
scope.convertDate();
});
scope.convertDate = function () {
if (scope.ngModel) {
if (scope.isValid()) {
} else {
scope.$apply(function () {
ctrl.$setValidity('invalidTime', false);
});
}
}
};
scope.isValid = function () {
var timeRegEx = /^ *(1[0-2]|[1-9]):[0-5][0-9] *(a|p|A|P)(m|M) *$/i;
return scope.ngModel.match(timeRegEx);
};
}
};
};
angular.module('common')
.directive('timePicker', [timePicker]);
})();
describe('Common Module', function () {
'use strict';
beforeEach(module('common'));
fdescribe('Time Picker Directive', function () {
var elem, scope, model, innerScope;
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
model = scope.ngModel;
var el = angular.element('<time-picker name="startTime" data-ng-model="model"/>');
elem = $compile(el)(scope);
scope.$apply();
innerScope = elem.isolateScope();
}));
describe('invalid times', function () {
it('should mark the field as invalid if the entered value has any alpha characters beside A, M and P', function () {
innerScope.ngModel = '1245PR';
innerScope.convertDate();
expect(elem.hasClass('.ng-invalid')).toBe(true);
});
})
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment