validators.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. angular.module('textAngular.validators', [])
  2. .directive('taMaxText', function(){
  3. return {
  4. restrict: 'A',
  5. require: 'ngModel',
  6. link: function(scope, elem, attrs, ctrl){
  7. var max = parseInt(scope.$eval(attrs.taMaxText));
  8. if (isNaN(max)){
  9. throw('Max text must be an integer');
  10. }
  11. attrs.$observe('taMaxText', function(value){
  12. max = parseInt(value);
  13. if (isNaN(max)){
  14. throw('Max text must be an integer');
  15. }
  16. if (ctrl.$dirty){
  17. ctrl.$validate();
  18. }
  19. });
  20. ctrl.$validators.taMaxText = function(viewValue){
  21. var source = angular.element('<div/>');
  22. source.html(viewValue);
  23. return source.text().length <= max;
  24. };
  25. }
  26. };
  27. }).directive('taMinText', function(){
  28. return {
  29. restrict: 'A',
  30. require: 'ngModel',
  31. link: function(scope, elem, attrs, ctrl){
  32. var min = parseInt(scope.$eval(attrs.taMinText));
  33. if (isNaN(min)){
  34. throw('Min text must be an integer');
  35. }
  36. attrs.$observe('taMinText', function(value){
  37. min = parseInt(value);
  38. if (isNaN(min)){
  39. throw('Min text must be an integer');
  40. }
  41. if (ctrl.$dirty){
  42. ctrl.$validate();
  43. }
  44. });
  45. ctrl.$validators.taMinText = function(viewValue){
  46. var source = angular.element('<div/>');
  47. source.html(viewValue);
  48. return !source.text().length || source.text().length >= min;
  49. };
  50. }
  51. };
  52. });