最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Bootstrap簡單實用的表單驗證插件BootstrapValidator用法實例詳解

 更新時間:2020年03月29日 11:29:11   作者:一枕江風  
這篇文章主要介紹了Bootstrap簡單實用的表單驗證插件BootstrapValidator用法,結(jié)合實例形式詳細分析了Bootstrap表單驗證插件BootstrapValidator基本功能、原理、用法及操作注意事項,需要的朋友可以參考下

本文實例講述了Bootstrap簡單實用的表單驗證插件BootstrapValidator用法。分享給大家供大家參考,具體如下:

Bootstrap是現(xiàn)在非常流行的一款前端框架,這篇來介紹一款基于Bootstrap的驗證插件BootstrapValidator。

先來看一下效果圖(樣式是不是還不錯O(∩_∩)O哈哈~)。

Bootstrapvalidator下載地址:https://github.com/nghuuphuoc/bootstrapvalidator/?

引入對應(yīng)的CSS和JS

<link rel="stylesheet" type="text/css" href="css/bootstrap.css" rel="external nofollow" />
<link rel="stylesheet" type="text/css" href="css/bootstrapValidator.css" rel="external nofollow" />
<script src="js/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="js/bootstrap.js" type="text/javascript"></script>
<script src="js/bootstrapValidator.js"></script>

添加驗證規(guī)則

使用HTML添加驗證。

對某一個標簽添加驗證規(guī)則,需要放在<div class="form-group"></div>標簽中,input標簽必須有name屬性值,此值為驗證匹配的字段。其實就是要符合bootstrap表單結(jié)構(gòu)。

<div class="form-group">
  <label class="col-md-2 control-label">學號</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="stuNumber" data-bv-notempty="true" data-bv-notempty-message="用戶名不能為空" />
  </div>
</div>

初始化bootstrapValidator。

<script type="text/javascript">
$('form').bootstrapValidator({
  //默認提示
  message: 'This value is not valid',
  // 表單框里右側(cè)的icon
  feedbackIcons: {
    valid: 'glyphicon glyphicon-ok',
    invalid: 'glyphicon glyphicon-remove',
    validating: 'glyphicon glyphicon-refresh'
  },
  submitHandler: function (validator, form, submitButton) {
    // 表單提交成功時會調(diào)用此方法
    // validator: 表單驗證實例對象
    // form jq對象 指定表單對象
    // submitButton jq對象 指定提交按鈕的對象
  }
});
</script>

效果圖。

使用data-bv-notempty 和 data-bv-notempty-message屬性就可以簡單進行非空驗證。data-bv-notempty 有值就進行非空驗證,data-bv-notempty-message 中的值為提示消息。

使用JS添加驗證

HTML樣式代碼。

<div class="form-group">
  <label class="col-md-2 control-label">姓名</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="name" />
  </div>
</div>
<div class="form-group">
  <label class="col-md-2 control-label">年齡</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="age" />
  </div>
</div>
<div class="form-group">
  <label class="col-md-2 control-label">電話</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="phoneNumber" />
  </div>
</div>
<div class="form-group">
  <label class="col-md-2 control-label">Email</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="email" />
  </div>
</div>
<div class="form-group">
  <label class="col-md-2 control-label">密碼</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="pwd" />
  </div>
</div>
<div class="form-group">
  <label class="col-md-2 control-label">確定密碼</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="pwd1" />
  </div>
</div>

JS驗證代碼,其中fields屬性中的值,需要和HTML標簽中的name值一樣,確定給那個標簽添加驗證。

<script type="text/javascript">
  $('form').bootstrapValidator({
    //默認提示
    message: 'This value is not valid',
    // 表單框里右側(cè)的icon
    feedbackIcons: {
      valid: 'glyphicon glyphicon-ok',
      invalid: 'glyphicon glyphicon-remove',
      validating: 'glyphicon glyphicon-refresh'
    },
    submitHandler: function (validator, form, submitButton) {
      // 表單提交成功時會調(diào)用此方法
      // validator: 表單驗證實例對象
      // form jq對象 指定表單對象
      // submitButton jq對象 指定提交按鈕的對象
    },
    fields: {
      username: {
        message: '用戶名驗證失敗',
        validators: {
          notEmpty: {   //不能為空
            message: '用戶名不能為空'
          },
          remote: {  //后臺驗證,比如查詢用戶名是否存在
            url: 'student/verifyUsername',
            message: '此用戶名已存在'
          }
        }
      },
      name: {
        message: '姓名驗證失敗',
        validators: {
          notEmpty: {
            message: '姓名不能為空'
          }
        }
      },
      age: {
        message: '年齡驗證失敗',
        validators: {
          notEmpty: {
            message: '年齡不能為空'
          },
          numeric: {
            message: '請?zhí)顚憯?shù)字'
          }
        }
      },
      phoneNumber: {
        message: '電話號驗證失敗',
        validators: {
          notEmpty: {
            message: '電話號不能為空'
          },
          regexp: {  //正則驗證
            regexp: /^1\d{10}$/,
            message: '請輸入正確的電話號'
          }
        }
      },
      email: {
        message: 'Email驗證失敗',
        validators: {
          notEmpty: {
            message: 'Email不能為空'
          },
          emailAddress: {   //驗證email地址
            message: '不是正確的email地址'
          }
        }
      },
      pwd: {
        notEmpty: {
          message: '密碼不能為空'
        },
        stringLength: {   //檢測長度
          min: 4,
          max: 15,
          message: '用戶名需要在4~15個字符'
        }
      },
      pwd1: {
        message: '密碼驗證失敗',
        validators: {
          notEmpty: {
            message: '密碼不能為空'
          },
          identical: {  //與指定控件內(nèi)容比較是否相同,比如兩次密碼不一致
            field: 'pwd',//指定控件name
            message: '兩次密碼不一致'
          }
        }
      }
    }
  });
</script>

效果如下。

AJAX后臺交互驗證,驗證用戶名是否存在。

<div class="form-group">
  <label class="col-md-2 control-label">用戶名</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="username" />
  </div>
</div>
 
<script type="text/javascript">
$('form').bootstrapValidator({
  //默認提示
  message: 'This value is not valid',
  // 表單框里右側(cè)的icon
  feedbackIcons: {
    valid: 'glyphicon glyphicon-ok',
    invalid: 'glyphicon glyphicon-remove',
    validating: 'glyphicon glyphicon-refresh'
  },
  submitHandler: function (validator, form, submitButton) {
    // 表單提交成功時會調(diào)用此方法
    // validator: 表單驗證實例對象
    // form jq對象 指定表單對象
    // submitButton jq對象 指定提交按鈕的對象
  },
  fields: {
    username: {
      message: '用戶名驗證失敗',
      validators: {
        notEmpty: {   //不能為空
          message: '用戶名不能為空'
        },
        remote: {  //后臺驗證,比如查詢用戶名是否存在
          url: 'student/verifyUsername',
          message: '此用戶名已存在'
        }
      }
    }
  }
});
</script>

后臺驗證返回格式必須為{“valid”, true or false} 的json數(shù)據(jù)格式。后臺verifyUsername驗證判斷方法。

@RequestMapping(value="/verifyUsername")
@ResponseBody
public Map verifyUsername(String username){
  Student stu = studentService.findByUsername(username);
  Map map = new HashMap();
  if (stu == null) {
    map.put("valid", true);
  }else{
    map.put("valid", false);
  }
  return map;
}

效果如下。

下面是幾個比較常見的驗證規(guī)則。

  • notEmpty:非空驗證;
  • stringLength:字符串長度驗證;
  • regexp:正則表達式驗證;
  • emailAddress:郵箱地址驗證(都不用我們?nèi)戉]箱的正則了~~)
  • base64:64位編碼驗證;
  • between:驗證輸入值必須在某一個范圍值以內(nèi),比如大于10小于100;
  • creditCard:身份證驗證;
  • date:日期驗證;
  • ip:IP地址驗證;
  • numeric:數(shù)值驗證;
  • url:url驗證;
  • callback:自定義驗證
  • Form表單的提交

關(guān)于提交,可以直接用form表單提交即可。

<div class="form-group">
  <div class="col-md-6 col-md-offset-2">
    <button id="btn" type="submit" class="btn btn-primary">提交</button>
  </div>
</div>

也可以通過AJAX提交,提交按鈕代碼和form表單的提交按鈕代碼一樣,通過id選中按鈕綁定點擊事件提交。

$("#btn").click(function () {  //非submit按鈕點擊后進行驗證,如果是submit則無需此句直接驗證
  $("form").bootstrapValidator('validate');  //提交驗證
  if ($("form").data('bootstrapValidator').isValid()) {  //獲取驗證結(jié)果,如果成功,執(zhí)行下面代碼
    alert("yes");  //驗證成功后的操作,如ajax
  }
});

效果圖,這里驗證通過后通過彈框提示的,方法中可以寫AJAX提交代碼。

頁面完整代碼。

<meta charset="UTF-8">
<form action="" class="form-horizontal">
  <div class="form-group">
    <label class="col-md-2 control-label">學號</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="stuNumber" data-bv-notempty="true" data-bv-notempty-message="用戶名不能為空" />
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">用戶名</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="username" />
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">姓名</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="name" />
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">年齡</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="age" />
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">電話</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="phoneNumber" />
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">Email</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="email" />
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">密碼</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="pwd" />
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">確定密碼</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="pwd1" />
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-6 col-md-offset-2">
      <button id="btn" type="submit" class="btn btn-primary">提交</button>
    </div>
  </div>
</form>
 
<script type="text/javascript">
  $(function () {
    $('form').bootstrapValidator({
      //默認提示
      message: 'This value is not valid',
      // 表單框里右側(cè)的icon
      feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
      },
      submitHandler: function (validator, form, submitButton) {
        // 表單提交成功時會調(diào)用此方法
        // validator: 表單驗證實例對象
        // form jq對象 指定表單對象
        // submitButton jq對象 指定提交按鈕的對象
      },
      fields: {
        username: {
          message: '用戶名驗證失敗',
          validators: {
            notEmpty: {   //不能為空
              message: '用戶名不能為空'
            },
            remote: {  //后臺驗證,比如查詢用戶名是否存在
              url: 'student/verifyUsername',
              message: '此用戶名已存在'
            }
          }
        },
        name: {
          message: '姓名驗證失敗',
          validators: {
            notEmpty: {
              message: '姓名不能為空'
            }
          }
        },
        age: {
          message: '年齡驗證失敗',
          validators: {
            notEmpty: {
              message: '年齡不能為空'
            },
            numeric: {
              message: '請?zhí)顚憯?shù)字'
            }
          }
        },
        phoneNumber: {
          message: '電話號驗證失敗',
          validators: {
            notEmpty: {
              message: '電話號不能為空'
            },
            regexp: {  //正則驗證
              regexp: /^1\d{10}$/,
              message: '請輸入正確的電話號'
            }
          }
        },
        email: {
          message: 'Email驗證失敗',
          validators: {
            notEmpty: {
              message: 'Email不能為空'
            },
            emailAddress: {   //驗證email地址
              message: '不是正確的email地址'
            }
          }
        },
        pwd: {
          notEmpty: {
            message: '密碼不能為空'
          },
          stringLength: {   //檢測長度
            min: 4,
            max: 15,
            message: '用戶名需要在4~15個字符'
          }
        },
        pwd1: {
          message: '密碼驗證失敗',
          validators: {
            notEmpty: {
              message: '密碼不能為空'
            },
            identical: {  //與指定控件內(nèi)容比較是否相同,比如兩次密碼不一致
              field: 'pwd',//指定控件name
              message: '兩次密碼不一致'
            }
          }
        }
      }
    });
 
    $("#btn").click(function () {  //非submit按鈕點擊后進行驗證,如果是submit則無需此句直接驗證
      $("form").bootstrapValidator('validate');  //提交驗證
      if ($("form").data('bootstrapValidator').isValid()) {  //獲取驗證結(jié)果,如果成功,執(zhí)行下面代碼
        alert("yes");  //驗證成功后的操作,如ajax
      }
    });
  })
</script>

到這里,BootstrapValidator驗證插件的方法已經(jīng)寫的很全面了。不足地方歡迎大家下方留言指出!

可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。

PS:關(guān)于bootstrap布局,這里再為大家推薦一款本站的在線可視化布局工具供大家參考使用:

在線bootstrap可視化布局編輯工具:
http://tools.jb51.net/aideddesign/layoutit

希望本文所述對大家基于bootstrap的程序設(shè)計有所幫助。

相關(guān)文章

最新評論

偃师市| 尖扎县| 齐河县| 黄山市| 扬州市| 文山县| 乌鲁木齐市| 方城县| 阿图什市| 湄潭县| 自贡市| 东辽县| 华阴市| 七台河市| 潮安县| 峨眉山市| 罗江县| 米脂县| 上虞市| 平利县| 彰化县| 平陆县| 禹城市| 宜都市| 广丰县| 古蔺县| 望谟县| 家居| 新源县| 宜君县| 乐亭县| 拜泉县| 迁西县| 广河县| 高雄县| 德庆县| 湘潭市| 嵊州市| 河北区| 叙永县| 林甸县|