AngularJS 表单验证问题

AngularJS form validation issues

我正在尝试使用 angular js 进行简单的表单验证。但这似乎不起作用,而且我没有得到我所缺少的东西。

HTML

<form role="form" class="ng-pristine ng-valid" name="registerForm">
                        <fieldset>
                            <div class="form-group">
                                <input class="form-control" placeholder="First Name" name="firstname" type="text"
                                        autofocus="" required ng-class="{'has-error': registerForm.firstname.$invalid}">
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Last Name" name="lastname" type="text" value="">
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="E-Mail" name="email" type="email" value="" ng-class="{'has-error': registerForm.email.$dirty}">
                                <div ng-show="registerForm.email.$dirty">Email Invalid</div>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Password" name="password" type="password" value="" autocomplete="off">
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="Organization Name" name="organizationName" type="text" value="">
                            </div>
                            <div class="form-group">
                                <select data-ng-options="item.Display for item in OrganizationTypes track by item.Value" data-ng-model="SelectOrganizationType" class="form-control">
                                    <option value="">-Select Organization Type-</option>
                                </select>
                            </div>
                           
                            <input type="button" value="Register" class="btn btn-lg btn-success btn-block" data-ng-click="Submit()" name="btnSubmit"/>
                           
                        </fieldset>
                    </form>

现在的问题是,如果我开始输入无效的电子邮件,它会显示错误消息 "Email Invalid"。但这并没有发生。

此外,当我在提交空白表格之前进行调试时,它显示 "registerForm.$valid = true"

猜猜我错过了什么。我需要包含任何模块吗?

这里是Plunker

嗯,我不确定,但我想你也必须像这样使用 $error$dirty:

<div ng-show="registerForm.email.$dirty && registerForm.email.$error.email">Email Invalid</div>

此外,添加 novalidate 以禁用默认浏览器验证,如下所示:

<form role="form" class="ng-pristine ng-valid" name="registerForm" novalidate>

首先,您需要 registerForm.email.$error.email 来触发 'invalid email' 警报。

其次,我想您必须将这些输入字段与 ng-model 绑定,但我不确定是否有必要。

还有一件事,将 'novalidate' 添加到表单元素。

 <div class="form-group">
      <input class="form-control" placeholder="E-Mail" name="email" type="email" value="" ng-model="" >
      <div ng-show="registerForm.email.$error.email">Email Invalid</div>
 </div>

你错过了一堆东西。首先,您需要将您的字段与模型相关联,以便对它们进行验证。其次,您访问错误的语法不正确。

Here is a working plunkr for what you are trying to do.

这是相关代码:

<body ng-controller="MainCtrl">
  <form role="form" name="registerForm" novalidate>
    <fieldset>
      <div class="form-group">
        <input class="form-control" placeholder="First Name" name="firstname" ng-model="firstname" type="text" autofocus="" ng-class="{'has-error': registerForm.firstname.$error.required}" required />
      </div>
      <div class="form-group">
        <input class="form-control" placeholder="Last Name" name="lastname" ng-model="lastname" type="text" value="">
      </div>
      <div class="form-group">
        <input class="form-control" placeholder="E-Mail" name="email" ng-model="email" type="email" value="" ng-class="{'has-error': registerForm.email.$error.pattern}" ng-pattern="/^\S+@\S+\.\S+$/i" required />
      </div>
      <div class="form-group">
        <input class="form-control" placeholder="Password" name="password" ng-model="password" type="password" value="" autocomplete="off">
      </div>
      <div class="form-group">
        <input class="form-control" placeholder="Organization Name" name="organizationName" ng-model="organizationName" type="text" value="">
      </div>
      <div class="form-group">
        <select data-ng-options="item.Display for item in OrganizationTypes track by item.Value" data-ng-model="SelectOrganizationType" class="form-control">
          <option value="">-Select Organization Type-</option>
        </select>
      </div>

      <input type="button" value="Register" class="btn btn-lg btn-success btn-block" data-ng-click="Submit()" name="btnSubmit" />

    </fieldset>
  </form>

  <div ng-show="registerForm.firstname.$error.required">You must enter a first name.</div>
  <div ng-show="registerForm.email.$error.pattern || registerForm.email.$error.required">Email Invalid</div>

</body>