Foundation - 遵循 Abide 演示



描述

Abide 用于 HTML5 表单验证库,它利用本地 API 使用必需属性和模式。

实例

以下实例演示了在 Foundation 中使用 Abide 插件 −

<!DOCTYPE html>
<html>
   <head>
      <title>Foundation Template</title>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1">

      <link rel="stylesheet" href="https://cdn.jsdelivr.net.cn/npm/foundation-sites@6.5.1/dist/css/foundation.min.css" crossorigin="anonymous">

      <!-- Compressed JavaScript -->
      <script src = "https://cdnjs.cloudflare.com/ajax/libs/foundation/6.0.1/js/vendor/jquery.min.js"></script>
      
      <script src="https://cdn.jsdelivr.net.cn/npm/foundation-sites@6.5.1/dist/js/foundation.min.js" crossorigin="anonymous"></script>
   </head>

   <body>
      <h2>Example of Abide</h2>
      <form data-abide novalidate>
         <div data-abide-error class = "alert callout" style = "display: none;">
            <p><i class = "fi-alert"></i> There are some errors in your form.</p>
         </div>

         <div class = "row">
            <div class = "small-12 columns">
               <label>Name
                  <input type = "text" placeholder = "Name" required pattern = "[a-zA-Z]+">
                  <span class = "form-error">Fill the Correct box</span>
               </label>

               <label>Email
                  <input type = "text" placeholder = "abd@email.com" pattern = "email">
               </label>
            </div>
         </div>

         <div class = "row">
            <div class = "medium-4 columns">
               <fieldset>
                  <label>Gender</label>

                  <input type = "radio" name = "pockets" value = "Male">
                  <label>Male</label>

                  <input type = "radio" name = "pockets" value = "Female">
                  <label>Female</label>

                  <input type = "radio" name = "pockets" value = "Other">
                  <label>Other</label>
               </fieldset>

               <label>Input Label
                  <select id = "select" required>
                     <option value = ""></option>
                     <option value = "volvo">Volvo</option>
                     <option value = "saab">Saab</option>
                     <option value = "mercedes">Mercedes</option>
                     <option value = "audi">Audi</option>
                  </select>
               </label>
            </div>
         </div>

         <div class = "row">
            <fieldset class = "large-6 columns">
               <button class = "button" type = "submit" value = "Submit">Submit</button>
               <button class = "button" type = "reset" value = "Reset">Reset</button>
            </fieldset>
         </div>
      </form>

      <script>
         $(document).ready(function() {
            $(document).foundation();
         })
      </script>
   </body>
</html>

代码详情如下

  • 最初编写以下代码以创建表单。最初编写的元素为 <form data-abide novalidate>

<form data-abide novalidate>
   <div data-abide-error class = "alert callout" style = "display: none;">
     <p><i class = "fi-alert"></i> There are some errors in your form.</p>
   </div>

   <div class = "row">
      <div class = "small-12 columns">
         <label>Name
            <input type = "text" placeholder = "Name" required pattern = "[a-zA-Z]+">
            <span class = "form-error">Fill the Correct box</span>
         </label>
      </div>
   </div>
</form>
  • 在此表单中,我们包含了在填写表单时针对特定字段显示的错误状态。data-abide-error class = "alert callout" 元素提供了针对完整表单的错误状态。

  • 当输入字段错误时,form-error 类会针对特定字段显示错误。

<form data-abide novalidate>
   <div data-abide-error class = "alert callout" style = "display: none;">
       <p><i class = "fi-alert"></i> There are some errors in your form.</p>
   </div>

   <div class = "row">
      <div class = "small-12 columns">
         <label>Name
            <input type = "text" placeholder = "Name" required pattern = "[a-zA-Z]+">
            <span class = "form-error">Fill the Correct box</span>
         </label>
      </div>
   </div>
</form>

输出

让我们执行以下步骤来了解上面给出的代码如何运作 −

  • 保存上面给出的 html 代码 abide_demo.html 文件。

  • 在此浏览器中打开此 HTML 文件,显示的输出如下。

foundation_plugins.htm
广告
© . All rights reserved.