Skip to content

Instantly share code, notes, and snippets.

@jurgenzz
Last active March 31, 2016 13:06
Show Gist options
  • Save jurgenzz/749dfcabd77fd4f6e5595f51b6cc7f8c to your computer and use it in GitHub Desktop.
Save jurgenzz/749dfcabd77fd4f6e5595f51b6cc7f8c to your computer and use it in GitHub Desktop.
var myApp = angular.module('myApp', [])
myApp.directive('phoneValidation', function(){
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngmodel) {
var newValue;
scope.$watch(attrs.ngModel, function (value) {
var newValue = value;
if (value) {
if (value.indexOf('+') === -1) {
value = value.replace(/[^0-9+]/g, '')
} else if (value.indexOf('+') === 0) {
value = '+' + value.replace(/[^0-9]/g, '');
} else if(value.indexOf('+') > 0) {
value = value.replace(/[^0-9]/g, '');
}
}
if(newValue !== value) {
ngmodel.$setViewValue(value);
ngmodel.$render();
}
})
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment