diff --git a/webadmin/src/js/index.js b/webadmin/src/js/index.js index e26caa83b..489ee0c7b 100644 --- a/webadmin/src/js/index.js +++ b/webadmin/src/js/index.js @@ -470,3 +470,35 @@ app.directive('tagInput', function () { '' }; }); + +app.directive('domainValidator', function() { + return { + + // limit usage to argument only + restrict: 'A', + + // require NgModelController, i.e. require a controller of ngModel directive + require: 'ngModel', + + // create linking function and pass in our NgModelController as a 4th argument + link: function(scope, element, attr, ngModel) { + function validator(value) { + + if (value.indexOf('.cloudron.') === -1 && /^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,3})$/.test(value)) { + ngModel.$setValidity('invalidDomain', true); + } else { + ngModel.$setValidity('invalidDomain', false); + } + + // we need to return our value, to be displayed to the user(value of the input) + return value ? value : undefined; + } + + //For DOM -> model validation + ngModel.$parsers.unshift(validator); + + //For model -> DOM validation + ngModel.$formatters.unshift(validator); + } + }; +});