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


    <p>&lt;%@ page contentType="text/html; charset=utf-8"%&gt; &lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;</p>
    <p>尽知网</p>
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <p>&nbsp;</p>
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <p>&nbsp;</p>
    <!--[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]--><!--header-->
    <div class="container-fluid">
    <div class="navbar-header"><button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span>    </button> <a class="navbar-brand" href="#"> <img class="img-responsive" src="/jinzhi/assets/images/jz-logo.png" alt="" /> </a></div>
    <div id="bs-example-navbar-collapse-1" class="collapse navbar-collapse">
    <ul id="header-nav" class="nav navbar-nav navbar-right">
    <li><a href="${ctx}/login">首页</a></li>
    <li class="jz-nav-center"><a href="#">个人中心</a></li>
    <li><a href="#">发布</a></li>
    <li class="cur"><a href="${ctx}/gotoLogin">登陆</a></li>
    <li><a href="${ctx}/gotoRegister">注册</a></li>
    </ul>
    </div>
    </div>
    <!--header end-->
    <p>&nbsp;</p>
    <!--登陆表单-->
    <div class="container">
    <div class="jz-login"><form id="jz-form-signin" class="form-signin" action="${ctx}/dologin" method="post" name="jz-form-signin"><input id="myusername" class="form-control" type="text" name="myusername" value="" />  <input id="mypassword" class="form-control" type="password" name="mypassword" value="" />
    <div class="jz-passward">
    <div class="row">
    <div class="col-xs-4">
    <div class="checkbox"><label> <input id="rememberMe" type="checkbox" checked="checked" /> 记住密码 </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="${ctx}/gotoRegister">注册</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">&nbsp;</div>
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script type="text/javascript" src="${ctx}/assets/common/jquery.js"></script>
    <script type="text/javascript" src="${ctx}/assets/common/bootstrap.min.js"></script>
    <script type="text/javascript" src="${ctx}/assets/common/jquery.validate.min.js"></script>
    <script type="text/javascript" src="${ctx}/assets/common/sweet-alert.min.js"></script>
    <script type="text/javascript" src="${ctx}/assets/common/jquery.cookie.js"></script>
    <script type="text/javascript">// <![CDATA[
        $(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);
                        var mobile_phone = $("#myusername").val();
                        var oms_password = $("#mypassword").val();
                        $.ajax({
                            type: "POST",
                            url: '${ctx}/dologin',
                            data: {mobile_phone,oms_password}/* $form.serializeArray() */,
                            beforeSend: function () {
                                $form.find(':submit').prop('disabled', true);
                            },
                            success: function (msg) {
                                
                                if(msg.code==201) {
                                    swal({
                                        title: "账户或密码不对",
                                        html: false,
                                        text: "重新填写账户和密码",
                                        type: "error",
                                        showCancelButton: false,
                                        confirmButtonText: "确定",
                                        closeOnConfirm: true
                                    });
                                    return
                                }
                                swal({
                                    title: "登陆成功",
                                    html: false,
                                    text: "登陆成功",
                                    type: "success",
                                    showCancelButton: false,
                                    confirmButtonText: "确定",
                                    closeOnConfirm: true
                                }, function(inputValue){
                                    window.location.href = "${ctx}/login"; //?"+Math.random();
                                    /*setTimeout(function() {
                                        swal('Ajax request finished!');
                                    }, 2000);*/
                                });
                            },
                            complete: function () {
                                $form.find(':submit').prop('disabled', false);
                            }
                        });
                    }
                });
            });
    
            /*记住密码*/
            var rmbUser = $("#rememberMe");
            if ($.cookie("rmbUser")) {
                rmbUser.attr("checked", "checked");
                $("#myusername").val($.cookie("userName"));
                $("#mypassword").val($.cookie("passWord"));
            }
            //保存用户信息
          rmbUser.click(function(){
               var userName = $("#myusername").val();
               var passWord = $("#mypassword").val();
               if (this.checked) {
                   $.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>

     



  • 相关阅读:
    MySql存储过程学习总结
    JAVA设计模式之策略模式
    JAVA设计模式之装饰模式
    JAVA设计模式之代理模式
    动手学servlet(四) cookie和session
    动手学servlet(三) 请求头和响应头信息
    动手学servlet(二) servlet基础
    动手学servlet(一) 第一个servlet程序
    HTTP与HttpServlet
    HttpServlet详解
  • 原文地址:https://www.cnblogs.com/wei-dong/p/7069541.html
Copyright © 2020-2023  润新知