Tuesday, November 24, 2015

Angular- forms

Resetting the values to default
  <body>
   <div ng-controller='sampleController'>
     <input name="firstname" ng-model="firstname"/>
     <button ng-click="reset()">reset</button>
   </div>
 
    <script>
      angular.module('myapp', [])
      .controller('sampleController',function($scope)
      {
        $scope.reset=function(){
          $scope.firstname="supreet"
        }
        $scope.reset();
      });
    </script>
  </body>









Form Validation for email field

  <body>
 
   <div>
     <form name="myform" novalidate>
   
     <input type="email" name="email" ng-model="email" length="100" minlength="3" maxlength="50" required/>
     <button>submit</button>
     <span ng-show="myform.email.$dirty && myform.email.$invalid">
     
       <div ng-show="myform.email.$error.required">Email is required</div>
       <div ng-show="myform.email.$error.email">Invalid format</div>
       <div ng-show="myform.email.$error.minlength">enter atleast 3 characters</div>
     
     </span>
           <button ng-disabled="myform.email.$dirty && myform.email.$invalid">submit</button>

    </form>
   
   </div>
 

  </body>






No comments:

Post a Comment