Link to home
Start Free TrialLog in
Avatar of chand pb
chand pbFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial