Bootstrap之form表单验证

bootstrap-3.3.7-dist
bootstrapvalidator-master 从这两个文件中引入js组件
html代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Document</title>
	<link rel="stylesheet" type="text/css" href="./bootstrap-3.3.7-dist/css/bootstrap.min.css">
	<link rel="stylesheet" href="./bootstrapvalidator-master/dist/css/bootstrapValidator.min.css">
	<script type="text/javascript" src = "./bootstrap-3.3.7-dist/js/jquery-3.3.1.min.js"></script>
	<script type="text/javascript" src="./bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="./bootstrapvalidator-master/dist/js/bootstrapValidator.min.js"></script>
</head>
<div class="container">
		<form class="form-horizontal" name="form1" action='reg1.php' method="post">

		  <div class="form-group">
		    <label for="user" class="col-sm-2 control-label">用户名:</label>
		    <div class="col-sm-10">
		      <input type="text" class="form-control" id="user" name="user" placeholder="请输入用户名">
		    </div>
		  </div>

  		  <div class="form-group">
		    <label for="age" class="col-sm-2 control-label">年龄:</label>
		    <div class="col-sm-10">
		      <input type="text" class="form-control" id="age" name="age" placeholder="请输入年龄">
		    </div>
		  </div>

		  <div class="form-group">
		    <label for="pwd" class="col-sm-2 control-label">Password:</label>
		    <div class="col-sm-10">
		      <input type="password" class="form-control" id="pwd" name="pwd" placeholder="请输入密码">
		    </div>
		  </div>

		  <div class="form-group">
		    <div class="col-sm-offset-2 col-sm-10">
		      <button type="submit" class="btn btn-primary btn-lg btn-block">注册</button>
		    </div>
		  </div>
		</form>
</div>

//js样式
notEmpty:非空验证;
numeric:数值验证;
stringLength:字符串长度;
regexp:正则表达式验证

$(function () {
        $('form').bootstrapValidator({
        message: 'This value is not valid',
             feedbackIcons: {
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                       },
            fields: {
            //user名称必须与name名称相同(区分大小写)
                user: {
                    message: '请输入用户名',
                    validators: {
                        notEmpty: {
                            message: '用户名不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 18,
                            message: '用户名长度必须在6到18位之间'
                        },
                    }
                },
                pwd: {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        }
                    }
                },
                age:{
                	validators:{
                		notEmpty: {
                            message: '年龄不能为空'
                        },
                		numeric:{
                			message:'必须是数值'
                		}
                	}
                }
            }
        });
    });


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值