首页 > 代码库 > 登陆jq表单验证及jqcookie记住密码实例

登陆jq表单验证及jqcookie记住密码实例

<!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">
    <title>尽知网</title>
    <link href="http://www.mamicode.com/css/bootstrap.css" rel="stylesheet">
    <link href="http://www.mamicode.com/css/font-awesome.css" rel="stylesheet">
    <link href="http://www.mamicode.com/css/sweet-alert.css" rel="stylesheet">
    <link href="http://www.mamicode.com/css/common.css" rel="stylesheet">
    <link href="http://www.mamicode.com/css/index.css" rel="stylesheet">
    <link href="http://www.mamicode.com/css/media.css" rel="stylesheet">
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn‘t work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body id="jz-login">
<!--header-->
<nav id="jz-nav" class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="http://www.mamicode.com/#">
                <img class="img-responsive" src="http://www.mamicode.com/images/jz-logo.png" >
            </a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><a class="cur" href="http://www.mamicode.com/尽知/index.html">首页</a></li>
                <li class="jz-nav-center"><a href="http://www.mamicode.com/#">个人中心</a></li>
                <li><a href="http://www.mamicode.com/#">发布</a></li>
                <li><a href="http://www.mamicode.com/尽知/login.html">登陆</a></li>
                <li><a href="http://www.mamicode.com/尽知/register.html">注册</a></li>
            </ul>
        </div>
    </div>
</nav>
<!--header end-->

<!--登陆表单-->
<div class="container">
    <div class="jz-login">
        <form class="form-signin" id="jz-form-signin" name="jz-form-signin" method="post" action="">
            <input name="myusername" id="myusername" value="" type="text" class="form-control" placeholder="账户:" required>
            <span class="jz-name-error"></span>
            <input name="mypassword" id="mypassword" value="" type="password" class="form-control" placeholder="密码:" required>
            <div class="jz-passward">
                <div class="row">
                    <div class="col-xs-4">
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" id="rememberMe"> 记住密码
                            </label>
                        </div>
                    </div>
                    <div class="col-xs-4"><a href="javascript:;">忘记密码</a></div>
                    <div class="col-xs-4"><a class="jz-register-btn pull-right" href="http://www.mamicode.com/尽知/register.html">注册</a></div>
                </div>
            </div>
            <button id="submit" class="btn btn-lg btn-primary btn-block" type="submit">登陆</button>
        </form>
    </div>
</div>
<!--登陆表单 end-->
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content jz-tip-form">

        </div>
    </div>
</div>
<!-- jQuery (necessary for Bootstrap‘s JavaScript plugins) -->
<script src="http://www.mamicode.com/common/jquery.js"></script>
<script src="http://www.mamicode.com/common/bootstrap.min.js"></script>
<script src="http://www.mamicode.com/common/jquery.validate.min.js"></script>
<script src="http://www.mamicode.com/common/sweet-alert.min.js"></script>
<script src="http://www.mamicode.com/common/jquery.cookie.js"></script>
<script type="text/javascript">
    $(function (){
        /*表单验证*/
        $("#submit").click(function() {
            /*表单验证  */
            $("#jz-form-signin").validate({
                rules: {
                    myusername: {
                        required: true
                    },
                    mypassword: {
                        required: true
                    }
                },
                messages: {
                    myusername: {
                        required: "请输入账户"
                    },
                    mypassword: {
                        required: "请输入密码"
                    }
                },
                submitHandler: function (form) {
                    var $form = $(form);
                    console.log( $form.serializeArray());
                    $.ajax({
                        type: "POST",
                        url: ‘‘,
                        data: $form.serializeArray(),
                        beforeSend: function () {
                            $form.find(‘:submit‘).prop(‘disabled‘, true);
                        },
                        success: function (json) {
                            if(json.errmsg == 1) {
                                swal({
                                    title: "账户和密码不对",
                                    html: false,
                                    text: "重新填写账户和密码",
                                    type: "success",
                                    showCancelButton: false,
                                    confirmButtonText: "确定",
                                    closeOnConfirm: true
                                });
                                return
                            }
                            swal({
                                title: "登陆成功",
                                html: false,
                                text: "登陆成功",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定",
                                closeOnConfirm: true
                            }, function(inputValue){
                                // window.location.hrefhttp://www.mamicode.com/= ""; //?"+Math.random();
                                /*setTimeout(function() {
                                    swal(‘Ajax request finished!‘);
                                }, 2000);*/
                            });
                        },
                        complete: function () {
                            $form.find(‘:submit‘).prop(‘disabled‘, false);
                        }
                    });
                }
            });
        });

        /*记住密码*/
        if ($.cookie("rmbUser") == "true") {
            $("#rmbUser").attr("checked", true);
            $("#user").val($.cookie("userName"));
            $("#pass").val($.cookie("passWord"));
        }
        saveUserInfo();
        //保存用户信息
        function saveUserInfo() {
            var rmbUser = $("#rememberMe");
            var userName = $("#myusername").val();
            var passWord = $("#mypassword").val();
            if (rmbUser.attr("checked") == true) {
                $.cookie("rmbUser", "true", { expires: 7 }); // 存储一个带7天期限的 cookie
                $.cookie("userName", userName, { expires: 7 }); // 存储一个带7天期限的 cookie
                $.cookie("passWord", passWord, { expires: 7 }); // 存储一个带7天期限的 cookie
            }
            else {
                $.cookie("rmbUser", "false", { expires: -1 });
                $.cookie("userName", ‘‘, { expires: -1 });
                $.cookie("passWord", ‘‘, { expires: -1 });
            }
        }

    });
</script>
</body>
</html>

 技术分享

 



登陆jq表单验证及jqcookie记住密码实例