1

I have a simple form where I have applied AngularJS validation on a input box containing number value. On submit, it works fine but when I click on Reset button, it gives the input box validation error message. Looks like it is calling ng-submit after ng-click of Reset button. I have tried resetting the value of input field in ng-click, removed type='Reset' and also used $setPristine() on the form. But nothing helps. Below is the code.

<form name="myForm" novalidate ng-submit="submitFilter()">
    Enter Age:
    <div>
        <input name="age" ng-class="{errorinput: submitted && myForm.age.$invalid }" class="form-control" ng-pattern="/^[0-9]+$/" placeholder="Age" type="text" ng-model="age" required>            
    </div>
    <span class="error" ng-style="{color:'red'}" ng-show="submitted && myForm.age.$invalid">Please enter a valid Age</span>
    <button type="submit">
        Submit
    </button>
    <button ng-click="reset(myForm)">
        Reset
    </button>        
</form>

Controller:

var original = $scope.age;
$scope.submitFilter = function () {
    if ($scope.filterForm.$valid) {     
    } else {
        $scope.submitted = true;
    }
};                
$scope.reset = function (myForm) {
    $scope.age = angular.copy(original);
    $scope.myForm.$setPristine();
};

Kindly help in getting rid of this error. Thanks in Advance.

2
  • 3
    Try adding the attribute type="button" to the reset button. By default a button in a form is a submit button. Commented Dec 22, 2014 at 14:24
  • Added but still getting validation error on clicking Reset button. Thanks. Commented Dec 22, 2014 at 16:46

1 Answer 1

2

Setting the reset button's type as 'button' should work. Following code works perfectly on my machine.

// Code goes here

app.controller('mainController', function($scope) {

 $scope.age = 123;
 var original = $scope.age;
 $scope.submitFilter = function () {
   $scope.submitted = true;
 };                
 $scope.reset = function (myForm) {
   $scope.age = angular.copy(original);
   $scope.myForm.$setPristine();
 };
});


  <form name="myForm" novalidate ng-submit="submitFilter()">
    Enter Age:
    <div>
        <input name="age" ng-class="{errorinput: submitted && myForm.age.$invalid }" class="form-control" ng-pattern="/^[0-9]+$/" placeholder="Age" type="text" ng-model="age" required>            
    </div>
    <span class="error" ng-style="{color:'red'}" ng-show="submitted && myForm.age.$invalid">Please enter a valid Age</span>
    <button type="submit">
        Submit
    </button>
    <button type="button" ng-click="reset(myForm)">
       Reset
    </button>        
  </form>
Sign up to request clarification or add additional context in comments.

3 Comments

You are using which version of AngularJS and Jquery.
@pogo22 Angularjs 1.3.8 and jquery 2.1.3, both are the latest version.
Added the code on Plunker with the latest versions available there. plnkr.co/edit/MpsrsTiUWAsgqquGZZP9?p=info Also added $scope.submitted = false; in reset function of Controller. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.