Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger form validation on ng-click

Tags:

angularjs

I created a Plunker that displays a simple form with a required email field. The validation works when the user types in the email but is there a way to trigger the validation when clicking on the button. I don't want to disable the button based on the form validity.

like image 246
Sydney Avatar asked Sep 02 '26 08:09

Sydney


1 Answers

Right now, you're binding ng-show to data that changes whenever the user types. Instead bind it to data that changes when the button is clicked. Here's my take on it:

$scope.update = function(user) {
    $scope.isEmptyEmail = $scope.form.uEmail.$error.required;
    $scope.isInvalidEmail = $scope.form.uEmail.$error.email;
};

And then bind your data to these new values.

  <span ng-show="isEmptyEmail">Tell us your email.</span>
  <span ng-show="isInvalidEmail">This is not a valid email.</span>
like image 178
Jon7 Avatar answered Sep 05 '26 12:09

Jon7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!