PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/email-checker-step4/app.js

https://gitlab.com/x33n/angular-101-from-n00b-to-ngExpert
JavaScript | 59 lines | 49 code | 10 blank | 0 comment | 4 complexity | ec26c63564ccb3a99076f5027a64fbd3 MD5 | raw file
  1. angular.module('emailChecker', []);
  2. angular.module('emailChecker')
  3. .provider('EmailService', function() {
  4. this.blacklist = [];
  5. this.setBlacklist = function(blacklist) {
  6. this.blacklist = blacklist;
  7. };
  8. this.$get = function() {
  9. var service = {};
  10. var config = this;
  11. service.isValid = function(email) {
  12. if (config.blacklist.indexOf(email) > -1) {
  13. return false;
  14. }
  15. var regex = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  16. return regex.test(email);
  17. }
  18. return service;
  19. }
  20. })
  21. .config(function(EmailServiceProvider) {
  22. EmailServiceProvider.setBlacklist(['gonto@gonto.com']);
  23. })
  24. .directive('emailCheckerButton', function() {
  25. return {
  26. restrict: 'E',
  27. replace: true,
  28. templateUrl: '/email-checker-step3/emailCheckerButton.html',
  29. scope: {
  30. label: '@',
  31. email: '='
  32. },
  33. controller: function($scope, EmailService) {
  34. $scope.checkEmail = function() {
  35. if (EmailService.isValid($scope.email)) {
  36. alert("It's a valid email :)!");
  37. } else {
  38. alert("It's an invalid email!");
  39. }
  40. }
  41. }
  42. }
  43. })
  44. .controller('MainCtrl', function($scope, EmailService) {
  45. $scope.$watch('email', function(newEmail) {
  46. if (!newEmail) $scope.validity = 'invalid';
  47. $scope.validity = EmailService.isValid(newEmail) ? 'valid' : 'invalid';
  48. })
  49. });