limitless.directive('isRequired', ['$parse', function ($parse) { return { restrict: 'A', link: function ($scope, el, attrs) { var model = attrs.model; var initValidate = attrs.validateOnInit || false; el.attr('valid', true); el.hide(); var validate=function(val) { var isValid = val !== undefined && val !== null && val !== ''; if (isValid) el.hide(); else el.show(); el.attr('valid', isValid); } $scope.$watch(model, function (newv, oldv) { if ((initValidate ) || (newv !==oldv)) { validate(newv); } }); $scope.$on('VALIDATE', function () { var val = $parse(model); validate(val($scope)); }); } } }]); limitless.directive('isMin', ['$parse',function ($parse) { return { restrict: 'A', link: function ($scope, el, attrs) { var model = attrs.model; var initValidate = attrs.validateOnInit || false; el.attr('valid', true); var min = parseFloat(attrs.isMin); el.hide(); var validate=function(val) { var isValid = parseFloat(val) >= min; if (isValid) el.hide(); else el.show(); el.attr('valid', isValid); } $scope.$watch(model, function (newv, oldv) { if ((initValidate) || (newv !== oldv)) { validate(newv); } }); $scope.$on('VALIDATE', function () { var val = $parse(model); validate(val($scope)); }); } } }]); limitless.directive('isNumeric', ['$parse',function ($parse) { return { restrict: 'A', link: function ($scope, el, attrs) { var model = attrs.model; var initValidate = attrs.validateOnInit || false; el.attr('valid', true); el.hide(); var validate=function(val) { var isValid = !isNaN(parseFloat(val)) && isFinite(val); if (isValid) el.hide(); else el.show(); el.attr('valid', isValid); } $scope.$watch(model, function (newv, oldv) { if ((initValidate) || (newv !== oldv)) { validate(newv); } }); $scope.$on('VALIDATE', function () { var val = $parse(model); validate(val($scope)); }); } } }]); limitless.directive('validation', ['Zupit.Core.AppService', function (app) { return { restrict: 'A', link: function ($scope, el, attrs) { var isPopup = $scope.isPopup || false; if ($scope.state!==undefined) $scope.state.originalModel = angular.copy($scope.state.model); $scope.form = { $isValid: function () { return $('[valid="false"]', el).length===0; }, $isDirty: function () { if ($scope.state === undefined) return false; return !angular.equals($scope.state.model, $scope.state.originalModel); }, validate:function() { $scope.$broadcast("VALIDATE"); }, $pristine: function (cancel) { if ($scope.state === undefined) return; if (cancel) $scope.state.model = $scope.state.originalModel; else $scope.state.originalModel=$scope.state.model; } }; if (!isPopup) { app.currentForm = $scope.form; } } } }]); limitless.directive('isEmail', ['$parse', function ($parse) { return { restrict: 'A', link: function ($scope, el, attrs) { var model = attrs.model; el.hide(); var initValidate = attrs.validateOnInit || false; el.attr('valid', true); function validateEmail(email) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return re.test(email); } var validate=function(val) { var isValid = validateEmail(val); if (isValid) el.hide(); else el.show(); el.attr('valid', isValid); } $scope.$watch(model, function (newv, oldv) { if ((initValidate) || (newv !== oldv)) { validate(newv); } }); $scope.$on('VALIDATE', function () { var val = $parse(model); validate(val($scope)); }); } } }]); limitless.directive('isInternationalPhone', ['$parse', function ($parse) { return { restrict: 'A', link: function ($scope, el, attrs) { var model = attrs.model; el.hide(); var initValidate = attrs.validateOnInit || false; el.attr('valid', true); function validateInternationalPhone(phone) { var re = /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}$/; return re.test(phone); } var validate=function(val) { var isValid = validateInternationalPhone(val); if (isValid) el.hide(); else el.show(); el.attr('valid', isValid); } $scope.$watch(model, function (newv, oldv) { if ((initValidate) || (newv !== oldv)) { validate(newv); } }); $scope.$on('VALIDATE', function () { var val = $parse(model); validate(val($scope)); }); } } }]); limitless.directive('hasExactLength', ['$parse',function ($parse) { return { restrict: 'A', link: function ($scope, el, attrs) { var model = attrs.model; var initValidate = attrs.validateOnInit || false; el.attr('valid', true); var length = parseFloat(attrs.hasExactLength); el.hide(); var validate=function(val) { var isValid = val.length === length; if (isValid) el.hide(); else el.show(); el.attr('valid', isValid); } $scope.$watch(model, function (newv, oldv) { if ((initValidate) || (newv !== oldv)) { validate(newv); } }); $scope.$on('VALIDATE', function () { var val = $parse(model); validate(val($scope)); }); } } }]);