基于Bootstrap+jQuery.validate Form表单验证实践
基于Bootstrap jQuery.validate Form表单验证实践
1、form 表单代码
- <!DOCTYPE html>
- <html>
- <head>
- <title>Bootstrap Form Template</title>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="stylesheet" type="text/css" href="plugins/bootstrap/css/bootstrap.css"/>
- </head>
- <body>
- <div class="container">
- <h1 class="text-center text-danger">Form 示例</h1>
- <form role="form" class="form-horizontal" action="javascript:alert('验证成功,可以提交.');" method="post">
- <div class="form-group">
- <label class="col-md-2 control-label" for="name">Name</label>
- <div class="col-md-10">
- <input class="form-control" name="name" type="text" id="name" placeholder="Name" value="" />
- </div>
- </div>
- <div class="form-group">
- <label class="col-md-2 control-label" for="exampleInputPassword1">Password</label>
- <div class="col-md-10">
- <input type="password" name="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
- </div>
- </div>
- <div class="form-group">
- <label for="intro" class="control-label col-md-2">Intro</label>
- <div class="col-md-10">
- <textarea id="intro" class="form-control" rows="3" name="intro" placeholder="Intro"></textarea>
- </div>
- </div>
- <div class="form-group">
- <label class="control-label col-md-2">Gender</label>
- <div class="col-md-10">
- <label class="radio-inline">
- <input type="radio" name="gender" value="男" />
- boy </label>
- <label class="radio-inline">
- <input type="radio" name="gender" value="女" />
- gril </label>
- </div>
- </div>
- <div class="form-group">
- <label for="hobby" class="control-label col-md-2">Hobby</label>
- <div class="col-md-10">
- <div class="checkbox">
- <label>
- <input type="checkbox" name="hobby" value="Music">
- Music</label>
- </div>
- <div class="checkbox">
- <label>
- <input type="checkbox" name="hobby" id="" value="Game" />
- Game </label>
- </div>
- <label class="checkbox-inline">
- <input type="checkbox" id="inlineCheckbox1" value="option1">
- option1 </label>
- <label class="checkbox-inline">
- <input type="checkbox" id="inlineCheckbox2" value="option2">
- option3</label>
- <label class="checkbox-inline">
- <input type="checkbox" id="inlineCheckbox3" value="option3">
- option3 </label>
- </div>
- </div>
- <div class="form-group">
- <label for="sel" class="control-label col-md-2">Select</label>
- <div class="col-md-10">
- <select multiple="" id="sel" name="sel" class="form-control">
- <option value="1">1</option>
- <option value="2">2</option>
- <option value="3">3</option>
- </select>
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <button type="submit" class="btn btn-primary btn-sm">
- Submit
- </button>
- <button type="reset" class="btn btn-primary btn-sm">
- Reset
- </button>
- </div>
- </div>
- </form>
- </div>
- <script src="plugins/jquery-1.11.1.js" type="text/javascript" charset="utf-8"></script>
- <script src="plugins/bootstrap/js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
- <script src="plugins/jquery-validation/dist/jquery.validate.js" type="text/javascript" charset="utf-8"></script>
- <script src="scripts/form.js" type="text/javascript" charset="utf-8"></script>
- <script type="text/javascript" charset="utf-8">
- MyValidator.init();
- </script>
- </body>
- </html>
需要引用 jquery.js,bootstrap.js,jquery.validate.js 库
2、form.js 代码
- var MyValidator = function() {
- var handleSubmit = function() {
- $('.form-horizontal').validate({
- errorElement : 'span',
- errorClass : 'help-block',
- focusInvalid : false,
- rules : {
- name : {
- required : true
- },
- password : {
- required : true
- },
- intro : {
- required : true
- }
- },
- messages : {
- name : {
- required : "Username is required."
- },
- password : {
- required : "Password is required."
- },
- intro : {
- required : "Intro is required."
- }
- },
- highlight : function(element) {
- $(element).closest('.form-group').addClass('has-error');
- },
- success : function(label) {
- label.closest('.form-group').removeClass('has-error');
- label.remove();
- },
- errorPlacement : function(error, element) {
- element.parent('div').append(error);
- },
- submitHandler : function(form) {
- form.submit();
- }
- });
- $('.form-horizontal input').keypress(function(e) {
- if (e.which == 13) {
- if ($('.form-horizontal').validate().form()) {
- $('.form-horizontal').submit();
- }
- return false;
- }
- });
- }
- return {
- init : function() {
- handleSubmit();
- }
- };
- }();
效果 :
基于Bootstrap+jQuery.validate Form表单验证实践的更多相关文章
- 基于Jquery Validate 的表单验证
基于Jquery Validate 的表单验证 jquery.validate.js是jquery下的一个验证插件,运用此插件我们可以很便捷的对表单元素进行格式验证. 在讲述基于Jquery Vali ...
- 异步提交form的时候利用jQuery validate实现表单验证
异步提交form的时候利用jQuery validate实现表单验证相信很多人都用过jquery validate插件,非常好用,并且可以通过下面的语句来自定义验证规则 // 电话号码验证 ...
- Jquery.validate.js表单验证插件的使用
作为一个网站web开发人员,以前居然不知道还有表单验证这样好呀的插件,还在一行行写表单验证,真是后悔没能早点知道他们的存在. 最近公司不忙,自己学习一些东西的时候,发现了validation的一个实例 ...
- jQuery.validate.js表单验证插件
jQuery.validate.js表单验证插件的使用 效果: 代码: <!DOCTYPE html> <html lang="en"> <head& ...
- jquery.validate.js 表单验证简单用法
引入jquery.validate.js插件以及Jquery,在最后加上这个插件的方法名来引用.$('form').validate(); <!DOCTYPE html PUBLIC " ...
- jquery.validate.js表单验证 jquery.validate.js的用法
jquery.validate.js这个插件已经用了2年多了,是一个不可多得的表单验证最方便快捷的插件.基于jquery的小插件,基本小白一学就会上手,对于项目表单页面比较多,元素比较多的校验,该插件 ...
- jquery.validate.js表单验证
一.用前必备官方网站:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ API: http://jquery.bassist ...
- 表单验证代码实例:jquery.validate.js表单验证插件
jquery.validate.js是JQuery旗下的一个验证插件,借助JQuery的优势,我们可以迅速验证一些常见的输入,并且可以自己扩充自己的验证方法.使用前请先下载必要的JQuery插件:jq ...
- jQuery Validate 插件[表单验证]
在客户端添加信息提交表单时我们经常需要做一些验证,比如验证不能为空,验证客户输入手机格式,验证客户输入email,url等的格式,我们可以通过EL表达式结合js 进行自主验证,今天总结一个JQuery ...
随机推荐
- hdu 3289 最大独立集
题意:一个动物园里有N只猫和K只狗,一些小朋友来参观,他们如果喜欢狗就不喜欢猫,喜欢猫就不喜欢狗,园长想要移走一些动物,如果,移走的是某个小朋友不喜欢的,而喜欢的没被移走,该小朋友就会高兴,求移动的数 ...
- Gunicorn部署部分的翻译
部署Gunicorn 文档建议Gunicorn最好是用在代理服务器后面.(等于前面最好加一个反向代理) Nginx Configuration 文档建议用Nginx,当然用其他也可以,但是要确保当你用 ...
- AMD K7以来核心架构一览表
转载或拿走使用请注明出处,谢谢! 注:K10以前AMD的CPU型号主要用PR值标称,故此表中未表示其准确型号
- ios数据保存
- Eclipse中执行Maven命令时控制台输出乱码
Maven 默认编码为 GBK: 在 Eclipse 控制台输出乱码: 解决方法:将以下代码添加到 pom.xml 的 <project> 节点下: <project> …… ...
- svn简单记录
记录一下工作中常用到的svn命令 一.文件的提交流程 1.svn up // 先更新本地文件 2.svn st // svn status 查看要提交的文件 3.#svn ci -m &quo ...
- UIScrollView 遇到的小坑
在做一个 UIScrollView 展示的时候 ,须要计算 contentSize 的高度,于是 我遍历了一下 UIScrollView 全部的子view的高度累加 然后得出 高度 .奇怪的是 发现 ...
- Freescale OSBDM JM60仿真器 BGND Interface
The BGND interface provides the standard 6 pin connection for the single wire BGND signal type devel ...
- STM32F4xx -- Cortex M4
STM32F4xx official page: http://www.st.com/internet/mcu/subclass/1521.jspIntroductionFPU - Floating ...
- bitnami下mysql配置-包含phpMyAdmin配置
mysql开启远程访问: 默认情况下mysql的绑定ip是bind-address=127.0.0.1 找到my.cnf bitnami@linux:~$ sudo find / -name my.c ...