22 lines
994 B
JavaScript
22 lines
994 B
JavaScript
// https://gist.github.com/Kashkovsky/86cfcccc47a07fb731571e84fa2b2442
|
|
// Note: We're using angularjs v1.2, so there is no $validators: https://code.angularjs.org/1.2.32/docs/guide/forms
|
|
angular.module('View.Directive')
|
|
.directive('phoneNumber', function () {
|
|
var phonePattern = /^\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$/;
|
|
return {
|
|
require: 'ngModel',
|
|
link: function (scope, elm, attrs, ctrl) {
|
|
ctrl.$parsers.unshift(function (viewValue) {
|
|
if (phonePattern.test(viewValue)) {
|
|
// it is valid
|
|
ctrl.$setValidity('phoneNumber', true);
|
|
return viewValue;
|
|
} else {
|
|
// it is invalid, return undefined (no model update)
|
|
ctrl.$setValidity('phoneNumber', false);
|
|
return undefined;
|
|
}
|
|
});
|
|
}
|
|
};
|
|
}); |