Bootstrap 表單驗證

2018-03-01 11:21 更新

表單驗證

Bootstrap還有三個input元素的驗證狀態(tài):

  • has-success將使標簽文本和邊框字段的顏色變?yōu)榫G色
  • has-error將使用棕色
  • has-warning將使用深紅色

下面是一個如何使用驗證狀態(tài)的示例:

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript"
  src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<link rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet"
  href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<script
  src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script
  src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $(".myDropdownHandle").dropdown("toggle");
});
</script>  
</head>
  <body style="margin:30px">
        <div class="form-group has-success">
           <label  class="control-label" 
                   for="inputField">Input with success</label>
           <input  type="text" class="form-control" id="inputField"/>
        </div>
  </body>
</html>

這些 has-*類型類只會將顏色應用于form-control control-label help-block類元素。

例子

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
<style type="text/css">
    .bs-example{
      margin: 20px;
    }
</style>
</head>
<body>
<div class="bs-example">
    <form class="form-horizontal">
        <div class="form-group has-success">
            <label class="col-xs-2 control-label" for="inputSuccess">Username</label>
            <div class="col-xs-10">
                <input type="text" id="inputSuccess" class="form-control" 
                placeholder="Input with success">
                <span class="help-block">Username is available</span>
            </div>
        </div>
        <div class="form-group has-warning">
            <label class="col-xs-2 control-label" for="inputWarning">Password</label>
            <div class="col-xs-10">
                <input type="password" id="inputWarning" class="form-control" 
                placeholder="Input with warning">
                <span class="help-block">Password strength: Weak</span>
            </div>
        </div>
        <div class="form-group has-error">
            <label class="col-xs-2 control-label" for="inputError">Email</label>
            <div class="col-xs-10">
                <input type="email" id="inputError" class="form-control" 
                placeholder="Input with error">
                <span class="help-block">Please enter a valid email address</span>
            </div>
        </div>
    </form>
</div>
</body>
</html>

feedback圖標

我們可以通過在.form-group和glyphicon上使用類.has-feedback,以在輸入框添加feedback圖標。

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> 
<style type="text/css">
    .bs-example{
      margin: 20px;
    }
</style>
</head>
<body>
<div class="bs-example">
    <form class="form-horizontal">
        <div class="form-group has-success has-feedback">
            <label class="col-xs-2 control-label" for="inputSuccess">Username</label>
            <div class="col-xs-10">
                <input type="text" id="inputSuccess" class="form-control" 
                placeholder="Input with success">
                <span class="glyphicon glyphicon-ok form-control-feedback"></span>
                <span class="help-block">Username is available</span>
            </div>
        </div>
        <div class="form-group has-warning has-feedback">
            <label class="col-xs-2 control-label" for="inputWarning">Password</label>
            <div class="col-xs-10">
                <input type="password" id="inputWarning" class="form-control" 
                placeholder="Input with warning">
                <span class="glyphicon glyphicon-warning-sign form-control-feedback"></span>
                <span class="help-block">Password strength: Weak</span>
            </div>
        </div>
        <div class="form-group has-error has-feedback">
            <label class="col-xs-2 control-label" for="inputError">Email</label>
            <div class="col-xs-10">
                <input type="email" id="inputError" class="form-control" placeholder="Input with error">
                <span class="glyphicon glyphicon-remove form-control-feedback"></span>
                <span class="help-block">Please enter a valid email address</span>
            </div>
        </div>
    </form>
</div>
</body>
</html>
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號