-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matthias Damm
committed
Mar 8, 2020
1 parent
0bf825d
commit 1d80650
Showing
6 changed files
with
69 additions
and
29 deletions.
There are no files selected for viewing
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
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
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
19 changes: 19 additions & 0 deletions
19
public/scripts/directives/ConvertToPhoneNumberDirective.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const convertToPhoneNumber = [ | ||
'$filter', | ||
function($filter) { | ||
return { | ||
require: 'ngModel', | ||
link: function(scope, elem, attrs, ctrl) { | ||
ctrl.$parsers.unshift(function(viewValue) { | ||
if (viewValue && viewValue[0] !== '+' && viewValue.length === 1) { | ||
elem.val(`+${viewValue}`); | ||
} else { | ||
return viewValue; | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
]; | ||
|
||
angular.module('convert-to-phone-number', []).directive('convertToPhoneNumber', convertToPhoneNumber); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,22 @@ | ||
angular.module('phone-number', []) | ||
.directive('phoneNumber', ['$parse', '$compile', function($parse, $compile) { | ||
const pattern = /^\+[0-9]{8,20}$/; | ||
const validatePhoneNumber = function($parse, $compile) { | ||
const pattern = /^\+[1-9]{1}[0-9]{7,20}$/; | ||
|
||
return { | ||
require: 'ngModel', | ||
link: function (scope, element, attrs, ctrl) { | ||
return { | ||
require: 'ngModel', | ||
link: function(scope, element, attrs, ctrl) { | ||
ctrl.$validators.integer = function(ngModelValue) { | ||
|
||
ctrl.$validators.integer = function (ngModelValue) { | ||
if(ngModelValue == undefined || ngModelValue == null){ | ||
ctrl.$setValidity('invalidPhone', true); | ||
return ngModelValue; | ||
} | ||
|
||
if (pattern.test(ngModelValue) == false) { | ||
ctrl.$setValidity('invalidPhone', false); | ||
return ngModelValue; | ||
} | ||
if (pattern.test(ngModelValue) == false) { | ||
ctrl.$setValidity('invalidPhone', false); | ||
return ngModelValue; | ||
} | ||
|
||
ctrl.$setValidity('invalidPhone', true); | ||
ctrl.$setValidity('invalidPhone', true); | ||
|
||
return ngModelValue; | ||
}; | ||
} | ||
}; | ||
|
||
}]) | ||
return ngModelValue; | ||
}; | ||
} | ||
}; | ||
}; | ||
|
||
angular.module('phone-number', []).directive('phoneNumber', validatePhoneNumber); |
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