Last active
March 31, 2016 13:06
-
-
Save jurgenzz/749dfcabd77fd4f6e5595f51b6cc7f8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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