Form validation on ng-click angularjs

后端 未结 2 1859
独厮守ぢ
独厮守ぢ 2020-12-14 16:05

I am unable to find a solution in the existing answers, hence i am posting this.

I have a form which has many input fields, many of them are required.

There

2条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 17:03

    You could set a flag so that you can show some kind of a required message or set some css class when the form is invalid.

    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.name=undefined;
        $scope.showMsgs = false;
        $scope.preview = function(form){
            if ($scope[form].$valid) {
                alert("Previewed");
            } else {
                $scope.showMsgs = true;
            }
        };
        $scope.update = function(form){
            if ($scope[form].$valid) {
                alert("Updated");
            } else {
                $scope.showMsgs = true;
            }    
        };
    });
    .error {
      border-color: red;
    }
    
    

    This field is required

提交回复
热议问题