textarea上的AngularJS ng-minlength / ng-maxlength显示错误消息但是在提交按钮单击时提交了表单

时间:2015-09-16 09:40:24

标签: angularjs validation angularjs-directive

    <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');
        });
    }

当我在textarea中输入低于50或超过200个字符的文本时,它显示错误消息但是表单在提交时单击提交按钮。 如果我没有输入任何表格,则表示未提交服务器显示错误信息。

我希望在显示错误消息时点击提交时不提交表单...

1 个答案:

答案 0 :(得分:0)

试试这段代码:

<强> HTML:

将表单名称作为参数传递到Submit()函数中。

<强> E.g。

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

<强> JS:

如果表单有效,请添加条件。

<强> 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');
        });
    }
};

希望这有帮助。