Bootstrap 表单验证formValidation 实现表单动态验证功能

yizhihongxing

下面将详细讲解 Bootstrap 表单验证 formValidation 实现表单动态验证功能的完整攻略。

什么是 Bootstrap 表单验证 formValidation

Bootstrap表单验证formValidation是一种基于jQuery的验证表单的插件。它是一个简单易用、灵活性强的工具,可以帮助开发者实现表单的动态验证功能。

formValidation可以对输入内容进行自定义验证,比如验证输入数据的长度、格式、是否符合正则表达式等,同时还可支持异步验证。

步骤

下面是利用Bootstrap 表单验证 formValidation实现表单动态验证功能的步骤:

  1. 在 HTML 文件中添加表单并引入Bootstrap表单验证formValidation的必要文件。
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Bootstrap 表单验证 formValidation 实现表单动态验证功能</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/formvalidation/0.6.1/css/formValidation.min.css">
  </head>
  <body>
    <div class="container">
      <form id="myForm" method="post" class="form-horizontal">
        <div class="form-group">
          <label class="col-md-3 control-label" for="firstName">First name</label>
          <div class="col-md-6">
            <input type="text" class="form-control" name="firstName" id="firstName" placeholder="Enter your first name">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label" for="lastName">Last name</label>
          <div class="col-md-6">
            <input type="text" class="form-control" name="lastName" id="lastName" placeholder="Enter your last name">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label" for="email">Email</label>
          <div class="col-md-6">
            <input type="text" class="form-control" name="email" id="email" placeholder="Enter your email address">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label" for="password">Password</label>
          <div class="col-md-6">
            <input type="password" class="form-control" name="password" id="password" placeholder="Enter your password">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label" for="password">Confirm Password</label>
          <div class="col-md-6">
            <input type="password" class="form-control" name="confirmPassword" id="confirmPassword" placeholder="Confirm your password">
          </div>
        </div>
        <div class="form-group">
          <div class="col-md-6 col-md-offset-3">
            <button type="submit" class="btn btn-primary btn-block">Sign up</button>
          </div>
        </div>
      </form>
    </div> 

    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.staticfile.org/formvalidation/0.6.1/js/formValidation.min.js"></script>
    <script src="https://cdn.staticfile.org/formvalidation/0.6.1/js/framework/bootstrap.min.js"></script>
  </body>
</html>
  1. 给表单添加自定义验证
<form id="myForm" method="post" class="form-horizontal">
  <div class="form-group">
    <label class="col-md-3 control-label" for="firstName">First name</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="firstName" id="firstName" placeholder="Enter your first name">
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label" for="lastName">Last name</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="lastName" id="lastName" placeholder="Enter your last name">
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label" for="email">Email</label>
    <div class="col-md-6">
      <input type="text" class="form-control" name="email" id="email" placeholder="Enter your email address">
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label" for="password">Password</label>
    <div class="col-md-6">
      <input type="password" class="form-control" name="password" id="password" placeholder="Enter your password">
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label" for="password">Confirm Password</label>
    <div class="col-md-6">
      <input type="password" class="form-control" name="confirmPassword" id="confirmPassword" placeholder="Confirm your password">
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-6 col-md-offset-3">
      <button type="submit" class="btn btn-primary btn-block">Sign up</button>
    </div>
  </div>
</form>
<script>
$('#myForm').formValidation({
    framework: 'bootstrap',
    icon: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: {
        firstName: {
            validators: {
                notEmpty: {
                    message: 'The first name is required'
                },
                stringLength: {
                    min: 2,
                    max: 30,
                    message: 'The first name must be more than 2 and less than 30 characters long'
                },
                regexp: {
                    regexp: /^[a-zA-Z\s]+$/i,
                    message: 'The first name can only consist of alphabetical characters and spaces'
                }
            }
        },
        lastName: {
            validators: {
                notEmpty: {
                    message: 'The last name is required'
                },
                stringLength: {
                    min: 2,
                    max: 30,
                    message: 'The last name must be more than 2 and less than 30 characters long'
                },
                regexp: {
                    regexp: /^[a-zA-Z\s]+$/i,
                    message: 'The last name can only consist of alphabetical characters and spaces'
                }
            }
        },
        email: {
            validators: {
                notEmpty: {
                    message: 'The email address is required'
                },
                emailAddress: {
                    message: 'The input is not a valid email address'
                }
            }
        },
        password: {
            validators: {
                notEmpty: {
                    message: 'The password is required and can\'t be empty'
                },
                stringLength: {
                    min: 6,
                    message: 'The password must have at least 6 characters'
                }
            }
        },
        confirmPassword: {
            validators: {
                notEmpty: {
                    message: 'The confirm password is required and can\'t be empty'
                },
                identical: {
                    field: 'password',
                    message: 'The password and confirm password is not the same'
                }
            }
        }
    }
});
</script>

以上代码演示了自定义验证规则。它主要包括指定元素 fields 以及验证器 validators。通过设置不同的验证器,你可以很容易地自定义表单的验证规则。

  1. 进行提交

当用户单击"Sign Up"按钮提交表单时,自动执行表单的验证程序,验证表单的输入合法性。

完整的示例代码:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Bootstrap 表单验证 formValidation 实现表单动态验证功能</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/formvalidation/0.6.1/css/formValidation.min.css">
  </head>
  <body>
    <div class="container">
      <form id="myForm" method="post" class="form-horizontal">
        <div class="form-group">
          <label class="col-md-3 control-label" for="firstName">First name</label>
          <div class="col-md-6">
            <input type="text" class="form-control" name="firstName" id="firstName" placeholder="Enter your first name">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label" for="lastName">Last name</label>
          <div class="col-md-6">
            <input type="text" class="form-control" name="lastName" id="lastName" placeholder="Enter your last name">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label" for="email">Email</label>
          <div class="col-md-6">
            <input type="text" class="form-control" name="email" id="email" placeholder="Enter your email address">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label" for="password">Password</label>
          <div class="col-md-6">
            <input type="password" class="form-control" name="password" id="password" placeholder="Enter your password">
          </div>
        </div>
        <div class="form-group">
          <label class="col-md-3 control-label" for="password">Confirm Password</label>
          <div class="col-md-6">
            <input type="password" class="form-control" name="confirmPassword" id="confirmPassword" placeholder="Confirm your password">
          </div>
        </div>
        <div class="form-group">
          <div class="col-md-6 col-md-offset-3">
            <button type="submit" class="btn btn-primary btn-block">Sign up</button>
          </div>
        </div>
      </form>
    </div> 

    <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdn.staticfile.org/formvalidation/0.6.1/js/formValidation.min.js"></script>
    <script src="https://cdn.staticfile.org/formvalidation/0.6.1/js/framework/bootstrap.min.js"></script>
    <script>
      $(document).ready(function() {
        $('#myForm').formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                firstName: {
                    validators: {
                        notEmpty: {
                            message: 'The first name is required'
                        },
                        stringLength: {
                            min: 2,
                            max: 30,
                            message: 'The first name must be more than 2 and less than 30 characters long'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z\s]+$/i,
                            message: 'The first name can only consist of alphabetical characters and spaces'
                        }
                    }
                },
                lastName: {
                    validators: {
                        notEmpty: {
                            message: 'The last name is required'
                        },
                        stringLength: {
                            min: 2,
                            max: 30,
                            message: 'The last name must be more than 2 and less than 30 characters long'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z\s]+$/i,
                            message: 'The last name can only consist of alphabetical characters and spaces'
                        }
                    }
                },
                email: {
                    validators: {
                        notEmpty: {
                            message: 'The email address is required'
                        },
                        emailAddress: {
                            message: 'The input is not a valid email address'
                        }
                    }
                },
                password: {
                    validators: {
                        notEmpty: {
                            message: 'The password is required and can\'t be empty'
                        },
                        stringLength: {
                            min: 6,
                            message: 'The password must have at least 6 characters'
                        }
                    }
                },
                confirmPassword: {
                    validators: {
                        notEmpty: {
                            message: 'The confirm password is required and can\'t be empty'
                        },
                        identical: {
                            field: 'password',
                            message: 'The password and confirm password is not the same'
                        }
                    }
                }
            }
        });
      });
    </script>
  </body>
</html>

示例

以下是两个用于表单验证的示例:

示例1: 对输入数据的长度限制进行验证

<div class="form-group">
  <label class="col-md-3 control-label" for="firstName">First name</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="firstName" id="firstName" placeholder="Enter your first name">
  </div>
</div>
firstName: {
    validators: {
        notEmpty: {
            message: 'The first name is required'
        },
        stringLength: {
            min: 2,
            max: 30,
            message: 'The first name must be more than 2 and less than 30 characters long'
        }
    }
}

在这个示例中,我们通过设置 minmax 属性,限制用户输入的 firstName 长度必须小于等于 30 个字符,大于等于 2 个字符。

示例2:对输入数据的格式限制进行验证

<div class="form-group">
  <label class="col-md-3 control-label" for="email">Email</label>
  <div class="col-md-6">
    <input type="text" class="form-control" name="email" id="email" placeholder="Enter your email address">
  </div>
</div>
email: {
    validators: {
        notEmpty: {
            message: 'The email address is required'
        },
        emailAddress: {
            message: 'The input is not a valid email address'
        }
    }
}

在这个示例中,我们通过设置 emailAddress 验证器来验证 email 的输入格式是否合法,不合法的话,给出一个提示信息: 'The input is not a valid email address'。

这个示例还展示了如何在验证器中指定出现错误时给出相应的提示信息。

结束语

至此,你应该已经学会了如何使用 Bootstrap 表单验证 formValidation 来实现表单的动态验证功能。希望你能够在实际开发中运用这些技术,为用户提供更加友好、便捷的应用程序。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Bootstrap 表单验证formValidation 实现表单动态验证功能 - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • JavaScript中的alert()函数使用技巧详解

    JavaScript中的alert()函数使用技巧详解 在JavaScript中,alert()函数用于弹出一个对话框,展示消息给用户。在本篇文章中,我们将详细讲解alert()函数的使用技巧。 基本用法 alert()函数是JavaScript的全局函数,调用它时无需使用任何前缀。例如: alert("Hello World!"); 上…

    JavaScript 2023年5月27日
    00
  • C#难点逐个击破(4):main函数

    C#难点逐个击破(4):main函数 什么是main函数 main() 是 C# 程序的入口点。每个 C# 控制台应用程序都必须拥有带有 static 关键字的 main() 函数。 当程序启动时,操作系统将运行可执行文件中的 main() 函数。 main函数的格式 main() 函数的格式如下: static void Main(string[] arg…

    JavaScript 2023年5月28日
    00
  • 利用JQUERY实现多个AJAX请求等待的实例

    当我们需要向服务器发送多个异步请求时,我们通常会使用jQuery的AJAX功能。但是当我们需要等待所有的请求都返回时才进行下一步操作时,该怎么办呢?这时,我们可以利用jQuery中的Promise对象来实现等待多个AJAX请求的处理。下面是利用jQuery实现多个AJAX请求等待的完整攻略。 基本使用方法 1. 创建多个deferred对象 我们可以使用jQ…

    JavaScript 2023年6月11日
    00
  • JavaScript 原型继承

    JavaScript 原型继承 JavaScript 原型继承是一种非常重要的概念,相较于传统的面向对象语言中的继承,JavaScript 通过原型继承来实现对象之间的属性和方法的共享,它是 JavaScript 中最为灵活的一种继承方式。 定义 JavaScript 中的每个对象(除了 null)都有一个原型对象,原型对象可以通过 Object.getPr…

    JavaScript 2023年6月10日
    00
  • JS课堂笔记(4.11-4.16)

    一、简单了解JS 1. JavaScript(简称JS)是作为开发Web页面的脚本语言。 2. JS是从1995年由网景公司的布兰德开发。 3. JavaScript的标准是ECMAScript。 4. JS代码是从上往下执行的。  二、变量 1. 变量名的值可以重复赋值(值可以修改),变量可以重复声明。 2. JS中“+”号很特殊,只要是和字符串相加都会变…

    JavaScript 2023年4月22日
    00
  • Javascript下的urlencode编码解码方法附decodeURIComponent

    下面是Javascript下的urlencode编码解码方法附decodeURIComponent的完整攻略,希望对您有所帮助。 什么是urlencode编码? urlencode编码是将字符转换为%xx形式的编码格式,其中xx表示字符编码的十六进制表示。urlencode编码可以用于处理URL中的特殊符号。如果URL中包含特殊符号,例如空格或换行符,则必须…

    JavaScript 2023年5月20日
    00
  • js仿iphone秒表功能 计算平均数

    那么接下来就为大家详细讲解一下“js仿iphone秒表功能 计算平均数”的完整攻略,具体步骤分为以下几个部分: 一、制作计时器功能 1.在HTML中创建一个div用来存放计时器所显示的时间; <div id="clock"></div> 2.在JS中定义计时器的初始值为0,定义一个变量来存储计时器的状态; var …

    JavaScript 2023年5月28日
    00
  • javascript 操作文件 实现方法小结

    Javascript 操作文件 实现方法小结 在Javascript中,操作文件的方法主要是使用File API和XMLHttpRequest对象的responseText、responseXML属性。 File API 1. 读取文件内容 使用File API的读取文件内容主要有以下几个步骤: 创建一个FileReader对象 调用FileReader对象…

    JavaScript 2023年5月27日
    00
合作推广
合作推广
分享本页
返回顶部