angularjs 表單驗證(不完整版)

來源:互聯網
上載者:User

標籤:連結   nova   oval   表單   false   invalid   取消   顯示   family   

針對項目實踐表單驗證總結:

angular 的 form表單驗證:form內需要novalidate取消預設驗證,用ng自己的驗證,form的名字是非常必要的

栗子:以註冊為栗子,下面是註冊的部分:

<form ng-submit=‘register‘ name=‘registerForm‘ novalidate>    <!-- 使用者名稱 -->    <div class="form-group" ng-class=‘{"error":registerForm.nickname.$invalid && submitted}‘>        <label>使用者名稱</label>        <input type="text" name="nickname" ng-model=‘nickname‘ required>        <div class="err-con"><span ng-show=‘registerForm.nickname.$error.required && submitted‘>請你輸入使用者名稱</span>        </div>    </div>    <!-- 密碼 -->    <div class="form-group" ng-class=‘{"error":registerForm.password.$invalid && submitted}‘>        <label>密碼</label>        <input type="text" name="password" ng-minlength=‘6‘ ng-model=‘password‘ required>        <div class="err-con"><span ng-show=‘registerForm.password.$invalid && submitted‘>輸入正確的密碼</span>        </div>    </div>    <!-- 確認密碼 -->    <div class="form-group" ng-class=‘{“error”:registerForm.confirmPassword.$invalid && submitted}‘>        <label>密碼</label>        <input type="text" name="confirmPassword" ng-minlength=‘6‘ ng-model=‘confirmPassword‘ required>        <div class="err-con"><span ng-show=‘password != confirmPassword && submitted‘>輸入密碼不一致</span></div>    </div></form>

對上面簡單解釋下:

1、ng-class是為了在錯誤驗證時為input加一個提示紅色的邊框。

2、submitted傳true或false與驗證共同控制是否錯誤提示顯示。

3、registerForm.password.$invalid 驗證是否符合規則,registerForm.nickname.$error.required判斷是否存在

4、formName.inputName.$invalid 或 formName.inputName.$error

5、在驗證碼再次點擊加一個ng-disabled, 手機正則加ng-pattern

6、ng-show判斷裡面是否符合規則顯示錯誤提醒,比jquery驗證可方便多啦

推薦連結:http://www.cnblogs.com/rohelm/p/4033513.html


 

 

angularjs 表單驗證(不完整版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.