• Angular Material表单提交及验证


    AngularJS中一些表单验证属性:

    1. 修改过的表单,只要用户修改过表单,无论输入是否通过验证,该值都将返回false
      {formName}.{inputFieldName}.$dirty
    2. 合法的表单,这个属性用来判断表单的内容是否合法的,如果合法则该属性的值为true
      {formName}.{inputFieldName}.$valid
    3. 不合法的表单,这个属性用来判断表单的内容是都不合法,如果不合法则该属性的值为true,与valid正好相反
      {formName}.{inputFieldName}.$invalid
    4. 错误,$error对象包含了当前表单的所有验证内容,以及它们是否合法的信息,如果验证失败,该属性值为true,如果验证成功,则该值为false
      {formName}.{inputFieldName}.$error
    5. form表单是否提交,该属性用来判断表单是否被用户提交
      {formName}.$submitted

    Angular Material中表单验证错误消息结合使用了ng-messages的用法。以下是一个简单例子及简单说明:

    点击提交按钮之后,form标签中ng-submit将表单的内容进行提交,js中进行是否合法判断;

    ng-pattern='/^正则表达式$/'  用来进行自定义表单验证,一般为正则表达式。

    ng-messages="{formName}.{inputFieldName}.$error" 用来验证该表单内容是否错误

    ng-message-exp=['required','minlength','maxlength','pattern']  这里是所需要验证的属性

    <form name="changePasswordForm" ng-submit="$ctrl.changePassword(changePasswordForm)" ng-cloak novalidate>
    <div> <label style="margin-right: 38px; margin-bottom: 0;">密码</label>   <md-input-container class="md-block no-margin" md-no-float>   <input name="password" type="password" ng-model="$ctrl.data.password" placeholder="请输入密码" style=" 300px" ng-pattern='/^+?[1-9]*d$/'
                          minlength="6" maxlength="20"
                          required/>
                   <div class="errors" ng-messages="changePasswordForm.password.$error">
                     <div ng-message-exp=['required','minlength','maxlength','pattern']>
                        您输入的密码格式不正确
                     </div>
                   </div>
               </md-input-container>
        </div>
      <md-button type="submit">提交</md-button>
    </form>
    
    <!--js-->
    this.changePassword=function(changePasswordForm){
      if(changePasswordForm.$invalid){
      //本次验证不合法
      return  
      }
    }
    

      

  • 相关阅读:
    Theano入门笔记1:Theano中的Graph Structure
    [译博文]CUDA是什么
    一天一经典Efficient Estimation of Word Representations in Vector Space
    Generating a Random Sample from discrete probability distribution
    关于representation的理解
    git代码管理——克隆项目到本地仓库及上传本地项目到仓库
    yum管理——linux字符界面安装图形化及两种界面的切换(3)
    yum管理——yum常用配置(2)
    yum管理——搭建iso镜像私有yum源仓库(1)
    apche编译安装
  • 原文地址:https://www.cnblogs.com/maoyazhi/p/7843053.html
Copyright © 2020-2023  润新知