Angular Js:验证用户输入的表单字段

时间:2015-08-13 10:05:53

标签: php angularjs

我有一个表单字段。问题是我想在用户与字段交互时显示错误。 我使用以下代码:

<form class="" name="myForm" novalidate>
      <input type="text" placeholder="First Name" class="user-icon" name="fname" ng-model="user.fname" required>
        <div ng-show="myForm.fname.$dirty && myForm.fname.$error.required">First Name is required.</div>

      <input type="text" placeholder="Last Name" class="user-icon" name="lname" ng-model="user.lname">
        <div ng-show="myForm.lname.$error.required">Last Name is required.</div>

      <input type="text" placeholder="Email" class="email-icon" name="email" ng-model="user.email">
        <div ng-show="myForm.email.$error.required">Email is required.</div>

      <input type="text" placeholder="Password" class="password-icon" name="password" ng-model="user.password">
        <div ng-show="myForm.password.$error.required">Password is required.</div>

      <input type="text" placeholder="Confirm Password" class="password-icon" name="conf_password" ng-model="user.conf_password">
        <div ng-show="myForm.conf_password.$error.required">Confirm Password is required.</div>


        </form>

但代码无效。如果我错了请纠正我?

1 个答案:

答案 0 :(得分:2)

您忘了添加required

并在表单中添加novalidate

试试这个

<input type="text" placeholder="First Name" class="user-icon" name="fname" ng-model="user.fname" required>