BootStrap表单验证实例代码

Bootstrap,来自 Twitter,是目前最受欢迎的前端框架。Bootstrap 是基于 HTML、CSS、JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷。

下面给大家分享bootstrap表单验证实例代码,具体代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="../include/taglib.jsp"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <%@include file="../include/common.jsp" %> <script type="text/javascript" src="https://www.jb51.net/form.js"></script> <script type="text/javascript" src="https://www.jb51.net/bootstrapValidator.min.js"></script> <link href=""> <link href="https://www.jb51.net/bootstrapValidator.min.css"> <title>表单测试</title> </head> <body> <div> <div> <div> <div> <div> <div> <form role="form"> <div> <label>用户姓名:</label> <div> <div> <input type="text" placeholder="用户姓名"/> </div> </div> </div> <!-- 生日--> <div> <label>生  日:</label> <div> <div> <input type="text" placeholder="生日"/> </div> </div> </div> <!-- 邮件 --> <div> <label>邮  件:</label> <div> <div> <input type="text" placeholder="邮件"/> </div> </div> </div> <!-- 密码 --> <div> <label>密  码:</label> <div> <div> <input type="text" placeholder="密码"/> </div> </div> </div> <!-- 确认密码 --> <div> <label>确认密码:</label> <div> <div> <input type="text" placeholder="确认密码"/> </div> </div> </div> <div> <label>语  言:</label> <div> <div> <input type="checkbox" value="中文"/>中文 </div> <div> <input type="checkbox" value="英语"/>英语 </div> <div> <input type="checkbox" value="德语"/>德语 </div> <div> <input type="checkbox" value="韩语"/>韩语 </div> </div> </div> <!-- 自定义 --> <div> <label></label> <div> <div> <input type="text" placeholder="自 定 义"/> </div> </div> </div> </form> <!-- PAGE CONTENT ENDS --> </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.page-content --> </div><!-- /.main-content-inner --> </div><!-- /.main-content --> <div></div> <div > <div> <button type="button"> 提交 </button> <button type="button"> 重置 </button> </div><!-- /.button-conent --> </div><!-- /.footer-button-box --> </div><!-- /.main-container --> </body> </html>

--------------------------------------------------引入js----------------------------------------------------------------------

$(function () { //重置 $('#resetBtn').click(function(){ //清空表单验证内容 $('#formTest')[0].reset(); //清空表单验证信息 $('#formTest').bootstrapValidator('resetForm'); }); function randomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }; $('#callbackshow').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' ')); //校验 $('#formTest').bootstrapValidator({ message : '', feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, //验证用户名字 fields: { username: { message: '用户名验证失败', validators: { notEmpty: { message: '用户名不能为空' }, stringLength: { min: 2, max: 4, message: '名字长度只能为2-4位' }, regexp: { regexp: /^[\u2E80-\u9FFF]+$/, message: '只能为汉字' }, } },//end username birthday:{ validators: { notEmpty: { message: '生日不能为空' }, date:{ format: 'YYYY/MM/DD', message:'输入正确的日期格式,YYYY/MM/DD' } } },//end birthday //验证email email: { validators: { notEmpty: { message: '邮箱地址不能为空' }, emailAddress: { message: '邮箱地址不对' } } },//end email password:{ validators: { notEmpty: { message: '密码不能为空' }, stringLength: { min: 6, max: 15, message: '密码长度最小为6最多为15' }, regexp: { regexp: /^[\d]+$/, message: '只能为数字' }, } },//end password confirmPassword:{ validators: { notEmpty: { message: '密码不能为空' }, stringLength: { min: 6, max: 15, message: '密码长度最小为6最多为15' }, identical: { field: 'password', message: '2次密码不一致' }, } },//end confirmPassword 'languages': { validators: { notEmpty: { message: '至少要选择一个' }, choice: { min: 2, max: 3, message: '选择2-3个' } } },//end languages callbacktest:{ validators: { callback:{ message:'输入错误', callback:function(value,validator){ var items = $('#callbackshow').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]); console.log(sum); return value == sum; } } } },//end callbacktest } }); //表单提交 $("#formTest").submit(function(ev){ev.preventDefault();}); $("#submitBtn").on("click", function(){ var bootstrapValidator = $("#formTest").data('bootstrapValidator'); bootstrapValidator.validate(); if(bootstrapValidator.isValid()){ $.ajaxRequest({ url : 'www.baidu.com', }); }else{ return; } }); });

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwdffj.html