Created
February 27, 2015 07:27
-
-
Save Studira/00d9adad43e2d2f84daf to your computer and use it in GitHub Desktop.
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'; | |
angular.module('app.validators.form') | |
.directive('validatorTelNumber',[ | |
function(){ | |
var link = function(scope, element, attrs, ngmodel){ | |
console.log('\n [Directive:ValidatorTelNumber]'); | |
console.log(' ---------------------------------------------- '); | |
console.info(element); | |
// -------------------------------------------------------------------------------- | |
// Attributes | |
// -------------------------------------------------------------------------------- | |
var self = element; | |
var PHONE_REGEXP = /^[0-9+\(\)#\.\s\/ext-]+$/; | |
// -------------------------------------------------------------------------------- | |
// Functions | |
// -------------------------------------------------------------------------------- | |
self.Initialize = function() { | |
console.log(' [*] ValidatorTelNumber: Initialising.'); | |
ngmodel.$parsers.push(parsePhoneNumber); | |
element.bind('keyup', bindEvent); | |
element.bind('blur', bindEvent); | |
console.log(' * self: ', self); | |
console.log(' ---------------------------------------------- '); | |
console.log('[Directive:ValidatorTelNumber - Initialized] \n\n'); | |
}; | |
function formatPhoneNumber(value){ | |
if(value){ | |
var number, area, front, end, parsedNumber = String; | |
var intCode = Boolean; | |
intCode = (value.substr(0,1) === '+') ? true : false; | |
number = value && value.replace(/[^0-9]+/g,''); | |
if(intCode){ number = 0 + number.slice(-9); } | |
area = number.substr(0,3); | |
front = number.substr(3,3); | |
end = number.substr(6,4); | |
parsedNumber = '(' + area + ')' + ' ' + front + ' ' + end; | |
return parsedNumber; | |
} | |
return undefined; | |
} | |
function bindEvent(event){ | |
var keyCode = event.keyCode; | |
var ignoredKeys = [8,46]; | |
if(ignoredKeys.indexOf(keyCode) === -1){ | |
var value = formatPhoneNumber(element.val()); | |
var isValid = !!value; | |
if(isValid){ | |
ngmodel.$setViewValue(value); | |
ngmodel.$render(); | |
} | |
ngmodel.$setValidity('telno', isValid); | |
scope.$apply(); | |
} | |
} | |
function parsePhoneNumber(value) { | |
if (value === '' || value === null || value === undefined) { return null; } | |
if (PHONE_REGEXP.test(value)) { | |
value = (value && value.replace(/[^0-9]+/g,'')).substr(0,10); | |
return value; | |
} | |
return undefined; | |
} | |
//Initialise directive | |
self.Initialize(); | |
}; //End of LINK function | |
// -------------------------------------------------------------------------------- | |
// Configuration | |
// -------------------------------------------------------------------------------- | |
return ({ | |
require : 'ngModel', | |
scope: {}, | |
restrict: 'A', | |
replace: false, | |
link: link, | |
}); | |
} //End of directive function | |
]); //End of directive | |
}()); //End of closure | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment