您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要为大家展示了“BootStrap怎么实现表单验证”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“BootStrap怎么实现表单验证”这篇文章吧。
Bootstrap,是目前最受欢迎的前端框架。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="form.js"></script> <script type="text/javascript" src="bootstrapValidator.min.js"></script> <link rel="stylesheet" href="../../css/edit.css"> <link rel="stylesheet" href="bootstrapValidator.min.css"> <title>表单测试</title> </head> <body class="no-skin"> <div class="main-container" id="main-container"> <div class="main-content"> <div class="main-content-inner"> <div class="page-content"> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <form id="formTest" class="form-horizontal" role="form"> <div class="form-group"> <label class="col-xs-3 col-sm-3 col-md-5 no-padding-right">用户姓名:</label> <div class="col-xs-3 col-sm-3 col-md-3"> <div> <input type="text" id="username" name="username" class="col-xs-12 col-sm-12 col-md-12" placeholder="用户姓名"/> </div> </div> </div> <!-- 生日--> <div class="form-group"> <label class="col-xs-3 col-sm-3 col-md-5 no-padding-right">生 日:</label> <div class="col-xs-3 col-sm-3 col-md-3"> <div> <input type="text" id="birthday" name="birthday" class="col-xs-12 col-sm-12 col-md-12" placeholder="生日"/> </div> </div> </div> <!-- 邮件 --> <div class="form-group"> <label class="col-xs-3 col-sm-3 col-md-5 no-padding-right">邮 件:</label> <div class="col-xs-3 col-sm-3 col-md-3"> <div> <input type="text" id="email" name="email" class="col-xs-12 col-sm-12 col-md-12" placeholder="邮件"/> </div> </div> </div> <!-- 密码 --> <div class="form-group"> <label class="col-xs-3 col-sm-3 col-md-5 no-padding-right">密 码:</label> <div class="col-xs-3 col-sm-3 col-md-3"> <div> <input type="text" id="password" name="password" class="col-xs-12 col-sm-12 col-md-12" placeholder="密码"/> </div> </div> </div> <!-- 确认密码 --> <div class="form-group"> <label class="col-xs-3 col-sm-3 col-md-5 no-padding-right">确认密码:</label> <div class="col-xs-3 col-sm-3 col-md-3"> <div> <input type="text" id="confirmPassword" name="confirmPassword" class="col-xs-12 col-sm-12 col-md-12" placeholder="确认密码"/> </div> </div> </div> <div class="form-group"> <label class="col-xs-3 col-sm-3 col-md-5 no-padding-right">语 言:</label> <div class="col-xs-3 col-sm-3 col-md-3"> <div> <input type="checkbox" id="languages" name="languages" value="中文" />中文 </div> <div> <input type="checkbox" id="languages" name="languages" value="英语" />英语 </div> <div> <input type="checkbox" id="languages" name="languages" value="德语" />德语 </div> <div> <input type="checkbox" id="languages" name="languages" value="韩语" />韩语 </div> </div> </div> <!-- 自定义 --> <div class="form-group"> <label class="col-xs-3 col-sm-3 col-md-5 no-padding-right" id="callbackshow"></label> <div class="col-xs-3 col-sm-3 col-md-3"> <div> <input type="text" id="callbacktest" name="callbacktest" class="col-xs-12 col-sm-12 col-md-12" placeholder="自 定 义"/> </div> </div> </div> </form> <!-- PAGE CONTENT ENDS --> </div><!-- /.col --> </div><!-- /.row --> </div><!-- /.page-content --> </div><!-- /.main-content-inner --> </div><!-- /.main-content --> <div class="clear-div"></div> <div class="footer-button-box container" > <div class="button-conent"> <button class="btn" type="button" id="submitBtn"> 提交 </button> <button class="btn" type="button" id="resetBtn"> 重置 </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; } }); });
以上是“BootStrap怎么实现表单验证”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。