sreehari
sreehari

Reputation: 89

AngularJS ng-minlength/ng-maxlength on textarea shows error message but form is getting submited on submit button click

    <form name="reviewForm" ng-submit="Submit();" novalidation>

    <div>
        <div class="label label-primary first">Your Review:</div>
        <div class="form-group has-error second">
            <textarea id="Description" name="Description" ng-minlength=50 ng-maxlength=200 ng-model="Description" ng-required="true" style="width: 500px; height: 200px;"></textarea>
             <label class="control-label" ng-show="submitted && reviewForm.Description.$error.required">Description is required.</label>
            <label class="control-label" ng-show="submitted && reviewForm.Description.$error.minlength">Description is should not be less than 50 charecters.</label>
            <label class="control-label" ng-show="submitted && reviewForm.Description.$error.maxlength">Description is should not be more than 200 charecters.</label>
        </div>
          <div style="clear:both;"></div>
    </div>

    <div>

        <div>
            <button type="submit" class="btn btn-primary" name="btnSubmit" ng-disabled="reviewForm.$invalid" ng-click="submitted=true">Submit</button>
        </div>
    </div>
    </form>

 $scope.Submit = function () {
        var product = {
            Title: $scope.Title,
            description: $scope.Description,
            name: $scope.Name,
            Rating: 3
        }
        var res = $http({
            url: "http://localhost:44826/api/review",
            method: "POST",
            data: $.param(product),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        });
        res.success(
            function (data, status, headers, config) {
                //console.error('Repos error', status, data);
                $scope.isVisible = false;
                $scope.FeedMsg = true;
                $scope.SuccessMsg = "Feedback has been submitted Sucessfully...";
            });
        res.error(function (data, status, headers, config) {
            alert('error');
        });
    }

When I entered text in textarea below 50 or above 200 characters, it is showing error message but the form is submitting while clicking on submit button. If I didn't enter anything form is not getting submitted to the server show error message.

I want the form not to submit on click of submit when error messages are showing...

Upvotes: 0

Views: 872

Answers (1)

Alberto I.N.J.
Alberto I.N.J.

Reputation: 1853

Try this code:

HTML:

Pass the form name as a parameter in your Submit() function.

E.g.

<form name="reviewForm" ng-submit="Submit(reviewForm);" novalidation>
    ...
</form>

JS:

Add a condition if the form is valid.

E.g.

$scope.Submit = function(reviewForm) {
    if (reviewForm.$valid) {
        var product = {
            Title: $scope.Title,
            description: $scope.Description,
            name: $scope.Name,
            Rating: 3
        };
        var res = $http({
            url: "http://localhost:44826/api/review",
            method: "POST",
            data: $.param(product),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        });
        res.success(
                function(data, status, headers, config) {
                    //console.error('Repos error', status, data);
                    $scope.isVisible = false;
                    $scope.FeedMsg = true;
                    $scope.SuccessMsg = "Feedback has been submitted Sucessfully...";
                });
        res.error(function(data, status, headers, config) {
            alert('error');
        });
    }
};

Hope this helps.

Upvotes: 0

Related Questions