I'm facing a couple of issues related to validation in AngularJS
Problem 1:
How can I achieve the following in the controller using AngularJS and not in the view?
vanilla js code
document.getElementById('InputId').value.length
I attempted the following in AngularJS
$scope.myForm.InputngModelName.length
Problem 2
I have a directive
that validates an email
How do I invoke it from my controller?
Here is the directive code snippet
angular.module('myValidator',[])
.directive('myValidator',function(){
return{
restrict: 'A',
require: "ngModel",
link: function(scope, elm, attrs, ctrl){
switch (attrs.myValidator){
case 'email':
var regex=/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/;
break;
}
ctrl.$parsers.unshift(function(viewValue){
if(regex.test(viewValue)){
ctrl.$setValidity('myValidator',true);
}
else{
ctrl.$setValidity('myValidator',false);
}
return viewValue;
});
}
};
});