AngularJS中的表单验证提交无效

时间:2013-12-18 09:30:06

标签: javascript forms validation angularjs

我正在尝试验证表单并在提交表单的文本字段下显示验证结果,如下所示。

但是我无法在提交时验证输入字段,但是我能够验证onBlur,我尝试了两种方式,分别参见onSubmit和onBlur的代码。

我的代码是:

  

formSumbit.html

<!DOCTYPE html>
<html lang="en" ng-app="testapp">
<head>
    <meta charset="utf-8"/>
    <title>BoilerPlate</title>
    <link rel="stylesheet" type="text/css" href="/reset.css"></link>
    <link rel="stylesheet" type="text/css" href="/lib/bootstrap/css/bootstrap.css"></link>


    <script src="/lib/angular/angular.js" ></script>
    <script src="/lib/angular/angular-route.js" ></script>
    <script src="/jquery.min.js" ></script>
    <script src="/lib/bootstrap/js/bootstrap.js" ></script>

    <script>
    var app=angular.module('testapp', ['ngRoute']);
     app.controller('signupController', ['$scope', function($scope) {
      $scope.submitted = false;
      $scope.signupForm = function() {
        console.log("hi");
        if ($scope.signup_form.$valid) {
          // Submit as normal
        } else {
          $scope.signup_form.submitted = true;
        }
      }
    }]);
    </script>
    <style type="text/css">
    .error{color:red}
    </style>

  </head>
  <body>
    <div>
            <form name="signup_form"  ng-submit="signupForm()" novalidate >
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" name="name"  ng-model="testform.name" required class="form-control" id="name" placeholder="Name"/>
                    <div class="error" ng-show="signup_form.name.$dirty && signup_form.name.$error.required && signup_form.submitted">
                        <small class="error">
                            Your name is required.
                        </small>

                    </div>
                </div>
                <div class="form-group">
                    <label for="email">Email address</label>
                    <input type="email" name="email"  ng-model="testform.email"  class="form-control" id="email" placeholder="Enter email"/>
                     <div class="error" ng-show="signup_form.email.$dirty && signup_form.email.$invalid && signup_form.submitted">
                        <small class="error" ng-show="signup_form.email.$error.email">
                                Your email not valid
                        </small>
                     </div>
               </div>
               <button type="submit" class="btn btn-default">Submit</button>
            </form>
    </div>
  </body>
</html>

但我可以在Blur事件中进行验证。请参阅下面的代码,该代码正在处理onBlur。

<!DOCTYPE html>
<html lang="en" ng-app="testapp">
<head>
    <meta charset="utf-8"/>
    <title>BoilerPlate</title>
    <link rel="stylesheet" type="text/css" href="/reset.css"></link>
    <link rel="stylesheet" type="text/css" href="/lib/bootstrap/css/bootstrap.css"></link>


    <script src="/lib/angular/angular.js" ></script>
    <script src="/lib/angular/angular-route.js" ></script>
    <script src="/jquery.min.js" ></script>
    <script src="/lib/bootstrap/js/bootstrap.js" ></script>

    <script>
    var app=angular.module('testapp', ['ngRoute']);
     app.controller('signupController', ['$scope', function($scope) {

    }]);
    </script>
    <style type="text/css">
    .error{color:red}
    </style>

  </head>
  <body>
    <div>
            <form name="signup_form"   novalidate >
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" name="name"  ng-model="testform.name" required class="form-control" id="name" placeholder="Name"/>
                    <div class="error" ng-show="signup_form.name.$dirty && signup_form.name.$error.required">
                        <small class="error">
                            Your name is required.
                        </small>

                    </div>
                </div>
                <div class="form-group">
                    <label for="email">Email address</label>
                    <input type="email" name="email"  ng-model="testform.email"  class="form-control" id="email" placeholder="Enter email"/>
                     <div class="error" ng-show="signup_form.email.$dirty && signup_form.email.$invalid">
                        <small class="error" ng-show="signup_form.email.$error.email">
                                Your email not valid
                        </small>
                    </div>
               </div>
               <button type="submit" class="btn btn-default">Submit</button>
            </form>
    </div>
  </body>
</html>

2 个答案:

答案 0 :(得分:3)

试试这个:

实施例

<form name="form" ng-app>
 <div class="control-group" ng-class="{true: 'error'}[submitted && form.email.$invalid]">
     <label class="control-label" for="email">Your email address</label>
      <div class="controls">
            <input type="email" name="email" ng-model="email" required />
            <span class="help-inline" ng-show="submitted && form.email.$error.required">Required</span>
            <span class="help-inline" ng-show="submitted && form.email.$error.email">Invalid email</span>
        </div>
    </div>

    <button type="submit" class="btn btn-primary btn-large" ng-click="submitted=true">Submit</button>
</form>

答案 1 :(得分:0)

尝试在容器div上设置控制器

<div ng-controller="signupController">

检查此示例 http://codepen.io/sevilayha/pen/xFcdI