Add angular directive for domain validation input fields

This commit is contained in:
Johannes Zellner
2017-01-26 14:46:19 -08:00
parent 3901144eae
commit 383d1eb406

View File

@@ -470,3 +470,35 @@ app.directive('tagInput', function () {
'</div>'
};
});
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);
}
};
});