trigger field validation in angular -- button click

  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 ||  
      $dirty &&$invalid"

Open in new window

here is my sample code

<!DOCTYPE html>

    <script data-require="angular.js@1.3.17" data-semver="1.3.17" src=""></script>
    <link rel="stylesheet" href="style.css" />
    <script src="app.js"></script>

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

<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>

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

   <button type="button" 

Open in new window

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

Open in new window

Thanks for your help
chand pbAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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:

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="$invalid && validating">

Open in new window

You may check the fork of your here.


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

From novice to tech pro — start learning today.