trigger field validation in angular -- button click

Hello,
  I have sample application that I would like the validate to be trigger when a button is click... In the example, I would like to see the error message if the validate button is press and the user did not complete the form

This is the validation that work for ng-disable... but now I want the button to always  be clickable  but do not proceed if the field are empty

 ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||  
                myForm.email.$dirty && myForm.email.$invalid"

Open in new window


here is my sample code

http://plnkr.co/edit/FHa6C7KFL6QgZdLZz0Xn?p=preview

index.html
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@1.3.17" data-semver="1.3.17" src="https://code.angularjs.org/1.3.17/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="app.js"></script>
  </head>

 <form ng-app="myApp" ng-controller="validateCtrl" 
name="myForm" novalidate>

<p>Username:<br>
<input type="text" name="user" ng-model="user" required>
<span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
<span ng-show="myForm.user.$error.required">Username is required.</span>
</span>
</p>

<p>Email:<br>
<input type="email" name="email" ng-model="email" required>
<span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
<span ng-show="myForm.email.$error.required">Email is required.</span>
<span ng-show="myForm.email.$error.email">Invalid email address.</span>
</span>
</p>

<p>
   <button type="button" 
      ng-click="">
                    Validate
                </button>
</form>
</body>
</html>

Open in new window


app.js
var app = angular.module('myApp', []);
app.controller('validateCtrl', function($scope) {
   
});

Open in new window


Thanks for your help
chand pbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:
Hi,

First of all if you do not want to have the red border on your fields then you need to add "novalidate" on your form declaration :

 <form ng-app="myApp" ng-controller="validateCtrl"  name="myForm" novalidate>

Open in new window


Then in order to do a validation only after the submit button has been clicked, then you need to introduce your own flag that would indicate the button submission.

On your app.js:

  $scope.validating = false;
  $scope.validate = function()
  {
    $scope.validating = true;
    
  };

Open in new window


So, when you load the form, the validating is false so it want show your messages. After you click on the submit button, it will raise the flag and allow normal validation.

On your fields:

<span style="color:red" ng-show="myForm.email.$invalid && validating">

Open in new window


You may check the fork of your http://plnkr.co/edit/qVtpJXTh3buYkWPS5s2g?p=previewplunker here.

Giannis
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.