bootstrap 表单添加表单验证

2021-11-16  本文已影响0人  shine001

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <title>表单验证</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <script type="text/javascript">
        $(function () {
      $("#form-demo").submit(function(event){
        const f=$(this)
        if(f[0].checkValidity()===false){
          event.preventDefault()
          event.stopPropagation()
        }
        f.addClass("was-validated")
      })

        })
    </script>
</head>
<body>
  <div class="container" style="margin-top: 200px;">
    <form action="" novalidate id="form-demo">
      <div class="form-group">
        <input type="text" class="form-control" required="required" placeholder="请输入用户名" pattern="[A-Za-z0-9]{6,30}">
        <div class="invalid-feedback"><small>请输入用户名6-30位字母或数字!</small></div>
      </div>
      <div class="form-group">
        <input type="text" class="form-control" required="required" pattern="[A-Za-z0-9]{6,30}" placeholder="请输入密码">
        <div class="invalid-feedback">密码长度至少为六位,只能是大小写字母或数字!</div>
      </div>
      <div class="form-group">
        <input type="text" class="form-control" required="required" pattern="[\u4e00-\u9fa5]{2,}" placeholder="请输入真实姓名">
        <div class="invalid-feedback">必须是中文,且长度不小于2位!</div>
      </div>
      <div class="form-group">
        <input type="text" class="form-control" required="required" pattern="1[34578]\d{9}$" placeholder="请输入手机号码">
        <div class="invalid-feedback">手机号只能是11位的数字!</div>
      </div>
      <div class="form-group">
        <input type="email" class="form-control" required="required"  placeholder="输入邮箱">
        <div class="invalid-feedback">邮箱地址格式不正确!</div>
      </div>
      <button class="btn btn-primary" type="submit">提交</button>
    </form>
  </div>
</body>
</html>

表单验证有提示语


<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
        <title>表单验证</title>

        <!-- Bootstrap core CSS -->
        <link href="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js" rel="stylesheet">
        <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    </head>
    <body>
        <div class="main" style="width: 80%; margin: 50px auto;">
            <h2>用户信息 表单验证</h2>

            <form id="updateform">
                <div class="form-group">
                    <label for="loginname" class="control-label">用户名:</label>
                    <input type="text" class="form-control" id="loginname" name="loginname">
                </div>
                <div class="form-group">
                    <label for="email" class="control-label">Email:</label>
                    <input type="text" class="form-control" id="email" name="email">
                </div>
                <div class="form-group">
                    <label for="password" class="control-label">密码:</label>
                    <input type="password" class="form-control" id="password" name="password">
                </div>
                <div class="form-group">
                    <label for="repassword" class="control-label">确认密码:</label>
                    <input type="password" class="form-control" id="repassword" name="repassword">
                </div>
                <div class="form-group">
                    <label for="phone" class="control-label">手机号码:</label>
                    <input type="text" class="form-control" id="phone" name="phone">
                </div>
                <div class="form-group">
                    <label for="address" class="control-label">收货地址:</label>
                    <textarea class="form-control" id="address" name="address"></textarea>
                </div>
                <div class="text-right">
                    <span id="returnMessage" class="glyphicon"> </span>
                    <!-- <button id="submitBtn" type="button" class="btn btn-primary">提交</button> -->
                    <button type="submit" id="submitBtn" class="btn btn-primary">注册</button>
                    <button type="reset" class="btn btn-default">重置</button>
                </div>
            </form>
        </div>

        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.slim.min.js"></script>
        <script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
        <script src="https://cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.js"></script>

        <script type='text/javascript'>
            var form = $('#updateform');
            $(document).ready(function() {

                form.bootstrapValidator({
                    message: '输入值不合法',
                    feedbackIcons: { //提示图标
                        valid: 'glyphicon glyphicon-ok',
                        invalid: 'glyphicon glyphicon-remove',
                        validating: 'glyphicon glyphicon-refresh'
                    },
                    fields: {
                        loginname: {
                            message: '用户名不合法',
                            validators: {
                                notEmpty: {
                                    message: '用户名不能为空'
                                },
                                stringLength: {
                                    min: 2,
                                    max: 20,
                                    message: '请输入2到20个字符'
                                },
                                regexp: {
                                    regexp: /^[a-zA-Z0-9_\. \u4e00-\u9fa5 ]+$/,
                                    message: '用户名只能由字母、数字、点、下划线和汉字组成 '
                                }
                            }
                        },
                        email: {
                            validators: {
                                notEmpty: {
                                    message: 'email不能为空'
                                },
                                emailAddress: {
                                    message: '请输入正确的邮件地址如:123@qq.com'
                                }
                            }
                        },

                        password: {
                            message: '以字母开头,长度在6-12之间,必须包含数字和特殊字符。',
                            validators: {
                                notEmpty: {
                                    message: '密码不能为空'
                                },
                                stringLength: {
                                    min: 6,
                                    max: 12,
                                    message: '请输入6到12个字符'
                                },
                                identical: { //相同
                                    field: 'password',
                                    message: '两次密码不一致'
                                },
                                different: { //不能和用户名相同
                                    field: 'username',
                                    message: '不能和用户名相同'
                                },
                                regexp: {
                                    regexp: /^[a-zA-Z][a-zA-Z0-9_]{6,12}$/,
                                    message: '密码以字母开头 由字母、数字、下划线和汉字组成 '
                                }
                            }
                        },
                        repassword: {
                            message: '必需以字母开头,长度在6-12之间',
                            validators: {
                                notEmpty: {
                                    message: '密码不能为空'
                                },
                                stringLength: {
                                    min: 6,
                                    max: 12,
                                    message: '密码长度必须在6到12之间'
                                },
                                identical: { //相同
                                    field: 'password',
                                    message: '两次密码不一致'
                                },
                                different: { //不能和用户名相同
                                    field: 'username',
                                    message: '不能和用户名相同'
                                },
                                regexp: { //匹配规则
                                    regexp: /^[a-zA-Z][a-zA-Z0-9_]{6,12}$/,
                                    message: '密码以字母开头 由字母、数字、下划线和汉字组成'
                                }
                            }
                        },


                        phone: {
                            validators: {
                                notEmpty: {
                                    message: '手机号码不能为空'
                                },
                                regexp: {
                                    regexp: "^([0-9]{11})?$",
                                    message: '手机号码格式错误'
                                }
                            }
                        },
                        address: {
                            validators: {
                                notEmpty: {
                                    message: '地址不能为空'
                                },
                                stringLength: {
                                    min: 8,
                                    max: 60,
                                    message: '请输入5到60个字符'
                                }
                            }
                        }
                    }
                });
            });
        </script>
    </body>
</html>

上一篇 下一篇

猜你喜欢

热点阅读