基于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 ...
随机推荐
- android Service oncreate 在UI线程 何时用service,何时用thread
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 服务的生命周期 各个方法 都是在主线程中的. 这里的操作可以导致主线程阻塞. 这些方法, ...
- 堆排序之Java实现
堆排序之Java实现 代码: package cn.com.zfc.lesson21.sort; /** * * @title HeapSort * @describe 堆排序 * @author 张 ...
- hdu 4417 区间内比h小的数 划分树
二分查找最近一个比h小的数 #include<cstdio> #include<iostream> #include<algorithm> #include< ...
- hashMap归纳
Hashmap的与hashtable的区别: Hashmap:允许key为空:查询速度快(他是非同步的:避免了同步中不必要的判断):不安全的(容易引 发多线程安全问题) Hashtable:不允许k ...
- Ural 2036. Intersect Until You're Sick of It 计算几何
2036. Intersect Until You're Sick of It 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2036 ...
- ConcurrentHashMap内存溢出问题
写在前面 上周,同事写了一段ConcurrentHashMap的测试代码,说往map里放了32个元素就内存溢出了,我大致看了一下他的代码及运行的jvm参数,觉得很奇怪,于是就自己捣鼓了一下.首先上一段 ...
- MikroTik RouterOS使用SATA光驱安装时提示:no CD-ROM found press ENTER to reboot
可以尝试以下方式: 1.进入bios--Main--Storage configuration,找到SATA configuration ,设置为Compatible模式(即兼容模式.混合模式) 2. ...
- Android 内存泄露测试数据处理--procrank,setprop,getprop(转)
1.Android内存测试常用的几个概念. VSS--virtual set size 虚拟耗用内存(包含共享库占用的内存)RSS--Resident set size实际使用的物理内存(包含共享库占 ...
- Versaloon -- Connect To Targets
Versaloon Full open-source(GPLv3) platform for multiple applications, including programmer, debugger ...
- Eclipse配置开发Go的插件——Goclipse
引言: 上篇 <Golang快速入门(不用急,但要快)> 我们大致过了一遍Go语言的基本语法,但在开始正式的项目创建前,有必要选择一个比较顺手的 IDE (编辑器),由于之前一直都是做Ja ...