javascript - How to call custom directive on form submit -
i have custom directive works when once change input value when submit form without changing input field value directive not display error message.
html code:
<form name="myform" role="form" class="form-horizontal" method="post" novalidate> <div class="form-group"> <label class="control-label col-sm-3" for="firstname">first name :</label> <div class="col-sm-9"> <input name='firstname' class="form-control" type='text' required ng-model='name' string> </div> </div> <div class="form-group"> <input type="submit" value="submit" class="btn btn-success" /> </div> </form>
directive :
validationmodule.directive('string', function () { return { restrict: 'a', require: 'ngmodel', link: function (scope, element, attr, ctrl) { function validationerror(value) { if (value.length < 1) { ctrl.$setvalidity('required', false); //showpopover(element, "required field"); errorvalidation(element, "required field"); // $(element).popover('show'); } if (/[a-za-z]/.test(value)) { ctrl.$setvalidity('invalid', true); // $(element).popover('destroy'); successvalidation(element); } if (/[0-9]/.test(value)) { ctrl.$setvalidity('invalid', false) errorvalidation(element, "number not allowed"); // showpopover(element, "number not allowed"); // $(element).popover('show'); } return value; } ctrl.$parsers.push(validationerror); } }; });
how can call directive perform validation on submit event ?
i don't know version of angular using, since 1.3 validators not hacked parsers. see $validators in https://docs.angularjs.org/api/ng/type/ngmodel.ngmodelcontroller using such generic names directives discouraged due possible name collisions. use prefix eg mystring
Comments
Post a Comment