Skip to content

Instantly share code, notes, and snippets.

@Kashkovsky
Created June 3, 2016 13:57
Show Gist options
  • Save Kashkovsky/86cfcccc47a07fb731571e84fa2b2442 to your computer and use it in GitHub Desktop.
Save Kashkovsky/86cfcccc47a07fb731571e84fa2b2442 to your computer and use it in GitHub Desktop.
AngularJS phone number validation directive
(function () {
'use strict';
var phonePattern = /^\+{0,1}[\d ()-]+$/;
app.directive('phoneNumber', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$validators.phoneNumber = function (modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)) {
return true;
}
if (phonePattern.test(viewValue)) {
return true;
}
return false;
};
}
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment