角度表单验证不起作用

时间:2015-03-17 11:45:50

标签: angularjs validation

我有以下表格:

更新

<script type="text/ng-template" id="form-profile.html">
            <form id="f1" name="form">
                <div class="form-group">
                    <label>Name</label>
                    <input type="text" class="form-control" name="name" ng-model="formData.name"  ng-class="submitted ? 'ng-dirty' : ' ' " required autofocus> 
                <span style="color:red" ng-show="form.name.$dirty || form.name.$invalid">Name is required</span>
                </div>
                <div class="form-group">
                    <label for="name">E-mail</label>
                    <input type="text" class="form-control" name="email" ng-model="formData.email" ng-class="submitted ? 'ng-dirty' : ' '" required autofocus>
                    <span style="color:red" ng-show="f1.email.$dirty && f1.email.$invalid">
                        <span ng-show="f1.email.$error.required">Email is required.</span>
                    </span>
                </div>
                <div class="form-group">
                    <label for="Cellphone">Mobil nr.</label>
                    <input type="text" class="form-control" name="Cellphone" ng-model="formData.Cellphone" ng-class="submitted ? 'ng-dirty' : ' '" required autofocus>
                    <span style="color:red" ng-show="f1.Cellphone.$dirty && f1.Cellphone.$invalid">
                        <span ng-show="f1.Cellphone.$error.required">Cellphone is required.</span>
                    </span>
                </div>

                <div class="form-group">
                    <label for="address">Adresse</label>
                    <input type="text" class="form-control" name="address" ng-model="formData.address">
                </div>
                <div class="form-group col col-33 col-offset-67">
                    <a ui-sref="front.form.organisation" class="button icon-right ion-chevron-right  button-energized .col-offset-50">
                        Next
                    </a>
                </div>
            </form>
        </script>

当我按下一个时,我希望表单禁用下一个按钮并显示错误。

到目前为止,我已尝试使用名称,但我没有收到错误。

谢谢

1 个答案:

答案 0 :(得分:1)

请参阅下面的演示

ng-show="f1.name.$dirty <-- f1 that's form name not id

var app = angular.module('app', []);

app.controller('homeCtrl', function($scope) {




});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="homeCtrl">

    <form name="f1">
      <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" name="name" ng-model="formData.name" required autofocus>

        <span style="color:red" ng-show="f1.name.$dirty  && f1.name.$invalid">Name is required</span>

      </div>
    </form>
  </div>