1. 程式人生 > 實用技巧 >jquery中用bootstrap中的表單驗證及提交

jquery中用bootstrap中的表單驗證及提交

Bootstrap:bootstrapValidator表單自定義驗證

例項:

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<!DOCTYPE html>
<html>
<head>
<title>11</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link type="text/css" rel="stylesheet" href="static/vendors/bootstrapvalidator/css/bootstrapValidator.css">
    <link type="text/css" rel="stylesheet" href="static/vendors/bootstrap/css/bootstrap.css">
</head> <body> <div class="container"> <br> <form class="form-horizontal" role="form" id="form-test"> <div class="form-group"> <label for="username" class="col-sm-1 control-label">姓名</label> <div class="col-sm-10"> <input type="text" class="form-control" id="username" name="username" placeholder="請輸入姓名"> </div> </div> <div class="form-group"> <label for
="password" class="col-sm-1 control-label">密碼</label> <div class="col-sm-10"> <input type="password" class="form-control" id="password" name="password" placeholder="請輸入密碼"> </div> </div> <div class="form-group"> <label for
="repassword" class="col-sm-1 control-label">確認密碼</label> <div class="col-sm-10"> <input type="repassword" class="form-control" id="repassword" name="repassword" placeholder="請輸入確認密碼"> </div> </div> <div class="form-group"> <label for="email" class="col-sm-1 control-label">郵箱</label> <div class="col-sm-10"> <input type="text" class="form-control" id="email" name="email" placeholder="請輸入郵箱"> </div> </div> <div class="form-group"> <div class="col-sm-offset-1 col-sm-10"> <div class="checkbox"> <label> <input type="checkbox">請記住我 </label> </div> </div> </div> <div class="form-group"> <div class="col-sm-offset-1 col-sm-10"> <button type="submit" class="btn btn-default" id="btn-test">登入</button> </div> </div> </form> </div> </body> <script src="static/vendors/jquery/js/jquery.min.js"></script> <script src="static/vendors/bootstrapvalidator/js/bootstrapValidator.js"></script> <script src="static/vendors/bootstrap/js/bootstrap.js"></script> <script> $(function () { $("#form-test").bootstrapValidator({ live: 'enabled',//驗證時機,enabled是內容有變化就驗證(預設),disabled和submitted是提交再驗證 excluded: [':disabled', ':hidden', ':not(:visible)'],//排除無需驗證的控制元件,比如被禁用的或者被隱藏的 submitButtons: '#btn-test',//指定提交按鈕,如果驗證失敗則變成disabled,但我沒試成功,反而加了這句話非submit按鈕也會提交到action指定頁面 message: '通用的驗證失敗訊息',//好像從來沒出現過 feedbackIcons: {//根據驗證結果顯示的各種圖示 valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, fields: { email: { validators: { emailAddress: {//驗證email地址 message: '不是正確的email地址' }, notEmpty: {//檢測非空 message: '請輸入郵箱' }, } }, password: { validators: { notEmpty: {//檢測非空 message: '請輸入密碼' }, } }, repassword: { validators: { notEmpty: {//檢測非空 message: '請輸入確認密碼' }, identical: {//與指定控制元件內容比較是否相同,比如兩次密碼不一致 field: 'password',//指定控制元件name message: '兩次輸入的密碼不同' }, } }, username: { validators: { notEmpty: {//檢測非空,radio也可用 message: '請輸入使用者名稱' }, stringLength: {//檢測長度 min: 3, max: 10, message: '長度必須在3-10之間' }, regexp: {//正則驗證 regexp: /^[a-zA-Z0-9_\.]+$/, message: '所輸入的字元不符要求' }, } } } }); function showToast(msg,shortCutFunction) { toastr.options = { "closeButton": true, "debug": false, "progressBar": true, "positionClass": "toast-bottom-right", "onclick": null, "showDuration": "400", "hideDuration": "1000", "timeOut": "7000", "extendedTimeOut": "1000", "showEasing": "swing", "hideEasing": "linear", "showMethod": "fadeIn", "hideMethod": "fadeOut" } toastr[shortCutFunction](msg,"提示"); } $("#btn-test").click(function () {//非submit按鈕點選後進行驗證,如果是submit則無需此句直接驗證 $("#form-test").bootstrapValidator('validate');//提交驗證 if ($("#form-test").data('bootstrapValidator').isValid()) {//獲取驗證結果,如果成功,執行下面程式碼 showToast("2345678","error"); alert("yes");//驗證成功後的操作,如ajax } }); }); </script>