demo20230512/Browser_Local/js/Directive/PhoneNumber.js

22 lines
994 B
JavaScript
Raw Permalink Normal View History

2023-05-12 10:20:28 +08:00
// 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;
}
});
}
};
});