I have developed a custom directive for validating input
. It is designed to check the length of the input. If the length is zero, an error message will be displayed. However, I am facing difficulty in hiding the error message when the input is filled with characters.
Check out my code below:
app.directive("formValidate", function() {
return {
require: 'ngModel',
template: '<p>Please Fill this Field</p>',
link: function(scope, elem, attr) {
scope.$watch(attr['ngModel'], function(value) {
if (((value || '').toString()).length == 0) {
var errorMessage = angular.element("<p class='error'>This field is requiured!</p>");
errorMessage.insertAfter(elem);
//$compile(errorMessage)(scope);
} else {
// What should I do now??
}
})
<input form-validate required md-no-asterisk ng-model="newEmployee.name" type="text" id="name" name="name">