角度输入验证不起作用

时间:2016-01-21 06:15:10

标签: angularjs validation

<form name="LPform">
    <div class="form-row clearfix">
         <label class="lbl-fld" style="margin-left: 12%;">Email ID</label>
         <input class="mob-adj-inpbx" type="email" name="uemail" ng-model="useremail" placeholder=" me@example.com" ng-required="true"/>
         <div ng-show="LPform.uemail.$valid">Enter a valid email.</div>
    </div>
</form>

在这里,我正在为“电子邮件”添加验证,并且仅在用户输入无效电子邮件时尝试显示错误消息。但这不起作用。

1 个答案:

答案 0 :(得分:1)

  1. 如果输入无效,我想你想显示错误信息

    ng-show="!LPform.uemail.$valid"

  2. 也许您忘了将它绑定到范围变量?因为它似乎工作正常。

  3. &#13;
    &#13;
    function Ctrl($scope) {
      $scope.useremail = '';
    }
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
    <div ng-app>
      <div ng-controller="Ctrl">
        <form name="LPform">
          <div class="form-row clearfix">
            <label class="lbl-fld">Email ID</label>
            <input class="mob-adj-inpbx" type="email" name="uemail" ng-model="useremail" placeholder=" me@example.com" ng-required="true" />
            <div style="font-size: 11px; color: red; margin: 5px 65px;" 
                 ng-show="!LPform.uemail.$valid">Please enter a valid email.</div>
          </div>
        </form>
      </div>
    </div>
    &#13;
    &#13;
    &#13;