Created
June 3, 2016 13:57
-
-
Save Kashkovsky/86cfcccc47a07fb731571e84fa2b2442 to your computer and use it in GitHub Desktop.
AngularJS phone number validation directive
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
(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