-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberOnlyDirective
More file actions
69 lines (56 loc) · 2.06 KB
/
numberOnlyDirective
File metadata and controls
69 lines (56 loc) · 2.06 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
angular.module('app').directive('numberOnly', [function () {
return {
require: 'ngModel',
link: function (scope, elem, attrs, ctrl) {
function inputFromUser(text) {
if (text) {
var transformedInput = text.replace(/[^0-9]/g, '');
if (transformedInput !== text) {
ctrl.$setViewValue(transformedInput);
ctrl.$render();
}
if(transformedInput.length == 0){
ctrl.$setPristine();
}
return transformedInput;
}
return undefined;
}
ctrl.$parsers.push(inputFromUser);
var type = attrs.numberOnly || "mobile";
// type - mobile or otp
if(type == 'otp'){
ctrl.$setValidity('otpValid', true);
}
elem.on('blur', function (evt) {
if(elem.val().length == 0){
return;
}
scope.$apply(function () {
if(type == 'otp'){
if(elem.val().length != 6) {
ctrl.$setValidity('otpValid', false);
}else{
ctrl.$setValidity('otpValid', true);
}
}
// for mobile
else if (/^[57-9]{1}[0-9]{9}$/.test(elem.val())) {
ctrl.$setValidity('mobileValid', true);
} else {
ctrl.$setValidity('mobileValid', false);
}
});
});
elem.on('focus', function (evt) {
scope.$apply(function () {
if(type == 'otp'){
ctrl.$setValidity('otpValid', true);
}else{
ctrl.$setValidity('mobileValid', true);
}
});
});
}
}
}]);