温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用jQuery的validate.js表单验证插件

发布时间:2020-07-10 16:38:39 来源:亿速云 阅读:133 作者:Leah 栏目:web开发

本篇文章为大家展示了如何使用jQuery的validate.js表单验证插件,代码简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

效果:

代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>表单验证插件Validate</title>
    <script src="js/jquery-1.10.2.min.js"></script>
    <script src="js/jquery.validate.min.js"></script>
    <style>
        body {
            background-color: #000;
        }
        
        form {
            width: 361px;
            margin: 80px auto;
            padding: 50px;
            border: 2px solid #666;
            box-shadow: 0 0 5px rgba(255, 255, 255, 0.2);
            background-color: #999;
            border-radius: 10px;
            box-sizing: border-box;
        }
        
        form>p {
            margin-bottom: 20px;
            color: #fff;
        }
        
        form>p>label {
            display: inline-block;
            width: 80px;
            text-align: center;
        }
        
        label.error {
            display: block;
            width: 100%;
            color: rgb(189, 42, 42);
            font-size: 12px;
            text-align: right;
            margin-top: 5px;
        }
        
        input {
            width: 170px;
            height: 20px;
            outline: none;
            background-color: #ddd;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        
        .submit {
            width: 170px;
            margin: 30px auto 0;
        }
        
        .submit input {
            background-color: #0099aa;
            color: #fff;
            border: 0;
            padding: 5px;
            height: 30px;
        }
    </style>
</head>

<body>
    <form id="signupForm" action="" method="post">
        <p>
            <label for="name">姓名:</label>
            <input type="text" id="name" name="name">
        </p>
        <p>
            <label for="email">邮箱:</label>
            <input type="email" id="email" name="email">
        </p>
        <p>
            <label for="password">密码:</label>
            <input type="password" id="password" name="password">
        </p>
        <p>
            <label for="confirm_password">确认密码:</label>
            <input type="password" id="confirm_password" name="confirm_password">
        </p>
        <p class="submit">
            <input type="submit" value="提交">
        </p>
    </form>
</body>
<script>
    $(function() {
        $("#signupForm").validate({
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                },
                password: {
                    required: true,
                    minlength: 5
                },
                confirm_password: {
                    required: true,
                    minlength: 5,
                    equalTo: "#password"
                }
            },
            messages: {
                name: "请输入姓名",
                email: {
                    required: "请输入Email地址",
                    email: "请输入正确的Email地址"
                },
                password: {
                    required: "请输入密码",
                    minlength: "密码不能小于5个字符"
                },
                confirm_password: {
                    required: "请输入确认密码",
                    minlength: "确认密码不能小于5个字符",
                    equalTo: "两次输入的密码不一致"
                }
            }
        });
    })
</script>

</html>

上述内容就是如何使用jQuery的validate.js表单验证插件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI