forked from akcoder/AngularJS-MacAddress-Directive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputToMacAddressDirective.js
More file actions
33 lines (29 loc) · 1 KB
/
InputToMacAddressDirective.js
File metadata and controls
33 lines (29 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
app.directive('macAddress', function () {
return {
restrict: "A",
require: "ngModel",
link: function (scope, ele, attr, ctrl) {
if (!ctrl) { return; }
var macAddressParse = function (value) {
return value.toUpperCase();
}
var macAddressFormat = function (value) {
if (!value) return undefined;
var numbers = value.replace(/-/g, "");
if (value.length % 3 === 0) {
return numbers.replace(/([0-9A-Za-z]{2})/g, "$1-");
}
}
ctrl.$parsers.push(macAddressParse);
ctrl.$formatters.push(macAddressFormat);
ele.on('input', function () {
var value = macAddressFormat(ele.val());
if (value !== undefined) {
ctrl.$setViewValue(value);
ctrl.$render();
}
scope.$apply();
});
}
};
});