Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular Custom Validation using Directive not working properly in ng- change

I have a custom validator using directives. im displaying validation using a method called by ng-change. Validation displays only in second change.

  <textarea id="txtEmail" name="emailId" type="text" class="form-control" 
           ng-change="$ctrl.validateForm()" ng-model-options="{allowInvalid: true}" 
           validate-Email="$ctrl.emailId" 
           ng-model="$ctrl.emailId" disabled-exception aria-labelledby="Email">
  </textarea>

Here validate-Email is the directive which use regex to validate email.

For ex: test@gmail.co , in this case if i remove last character ‘o’ , it will not display error validation. but if i remove last two character ‘co’ it will display proper validation.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

validateForm method is like

         ` vm.validateForm = function () {
                var errors = [];
                angular.forEach(vm.form.$error, function (item, key: string) {
                angular.forEach(item, function (errorField) {
                                    errors.push("Email Error");
                }`

Expecting solution to display validation properly.

>Solution :

First write a function like

vm.validate = function () {
    $timeout.cancel(callValidation);
    callValidation = $timeout(vm.validateForm, 1000);

}

then call the "vm.validate" function in textarea instead of "vm.validateForm".

I hope this will slove your query.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading