title: Validate Form with AngularJS on Enter keypress
body: <p>I have a description of field in my AngularJS code:</p>
  
  <pre><code>    input.form-control(type='password' placeholder='password' id='passwordInput'                                        
      name='passwordInput' ng-model='credentials.password
      ng-enter='loginByEnter(credentials,loginForm.$valid)'                                          
      ng-maxlength='{{passwordMaxLen}}'   required form-control)
  </code></pre>
  
  <p>The line in question is ng-enter one. The function loginByEnter is:</p>
  
  <pre><code>$scope.loginByEnter = function(credentials,htmlValidated) {
    if (htmlValidated) {   
      $scope.login(credentials);   
    } 
  };
  </code></pre>
  
  <p>And custom directive ng-enter is </p>
  
  <pre><code>.directive('ngEnter', function () { 
   return function (scope, element, attrs) {   
      element.bind("keydown keypress", function (event) {  
             if(event.which === 13) {  
                  scope.$apply(function (){    
                      scope.$eval(attrs.ngEnter);  
                   });     
              event.preventDefault();  
              }    
      });  
  </code></pre>
  
  <p>The purpose of all this: I want user to proceed with login when he netered password and hit Enter (so he does not have to press separate Login button elsewhere). But I also need to validate the form. Can it be done more elegantly, without querying a validation function on hitting enter?</p>