Show red border for all invalid fields after submitting form angularjs

后端 未结 3 852
清酒与你
清酒与你 2021-02-01 13:57

I have a form in which I have some input fields. Some of them are required fields and some are email fields.

I am using HTML5

3条回答
  •  执念已碎
    2021-02-01 14:26

    I have created a working CodePen example to demonstrate how you might accomplish your goals.

    I added ng-click to the

    and removed the logic from your button:

    
    ...
    
    

    Here's the updated template:


    First Name is required

    Last Name is required

    Email address is required Email address is not valid

    and controller code:

    app.controller('MainCtrl', function($scope) {  
      $scope.save = function(model) {
        $scope.addRelation.submitted = true;
    
        if($scope.addRelation.$valid) {
          // submit to db
          console.log(model); 
        } else {
          console.log('Errors in form data');
        }
      };
    });
    

    I hope this helps.

提交回复
热议问题