• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

登录验证码

武飞扬头像
潮阿景
帮助1

登录验证是一般系统都会有的功能,验证的方式也多种多样,比如输入式验证码,拖动式验证条,拖动式验证拼图等等。

我们这里先实现常规的输入验证码的方式,右边显示验证码图片,点击可刷新,左边输入验证码。如下图为实现的效果。

  1.  
    <!DOCTYPE html>
  2.  
    <html lang="en">
  3.  
    <head>
  4.  
    <style>
  5.  
    #code {
  6.  
    color:#1E9FFF;
  7.  
    font-weight: bold;
  8.  
    font-style:oblique;
  9.  
    letter-spacing:5px;
  10.  
    font-size:20px;
  11.  
    height: 2.8rem;
  12.  
    }
  13.  
    #input_code {
  14.  
    width: 63%;
  15.  
    background: #ffffff;
  16.  
    height: 2.8rem;
  17.  
    padding: 0 2%;
  18.  
    border-radius: 5px;
  19.  
    border: none;
  20.  
    border: 1px solid rgba(0,0,0,.2);
  21.  
    font-size: 0.9rem;
  22.  
    }
  23.  
    </style>
  24.  
     
  25.  
    <script>
  26.  
    if(window!==top)
  27.  
    top.location.replace(location.href);
  28.  
    </script>
  29.  
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  30.  
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  31.  
    <link rel="stylesheet" href="/static/assets/libs/layui/css/layui.css"/>
  32.  
    {# <link rel="stylesheet" href="{{ url_for('static',filename='plugin/layui/css/layui.css')}}"/>#}
  33.  
    {# <link rel="stylesheet" href="{{ url_for('static',filename='css/admin.css') }}?v=318"/>#}
  34.  
    <title>系统登陆</title>
  35.  
    <style>
  36.  
    body {
  37.  
    background-image: url("{{ url_for('static',filename='images/bg-login2.jpg') }}");
  38.  
    background-repeat: no-repeat;
  39.  
    background-size: cover;
  40.  
    min-height: 100vh;
  41.  
    }
  42.  
     
  43.  
    body:before {
  44.  
    content: "";
  45.  
    background-color: rgba(0, 0, 0, .2);
  46.  
    position: fixed;
  47.  
    top: 0;
  48.  
    left: 0;
  49.  
    right: 0;
  50.  
    bottom: 0;
  51.  
    }
  52.  
     
  53.  
    .login-wrapper {
  54.  
    max-width: 420px;
  55.  
    padding: 20px;
  56.  
    margin: 0 auto;
  57.  
    position: relative;
  58.  
    box-sizing: border-box;
  59.  
    z-index: 2;
  60.  
    }
  61.  
     
  62.  
    .login-wrapper > .layui-form {
  63.  
    padding: 25px 30px;
  64.  
    background-color: #fff;
  65.  
    box-shadow: 0 3px 6px -1px rgba(0, 0, 0, 0.19);
  66.  
    box-sizing: border-box;
  67.  
    border-radius: 4px;
  68.  
    }
  69.  
     
  70.  
    .login-wrapper > .layui-form > h2 {
  71.  
    color: #333;
  72.  
    font-size: 18px;
  73.  
    text-align: center;
  74.  
    margin-bottom: 25px;
  75.  
    }
  76.  
     
  77.  
    .login-wrapper > .layui-form > .layui-form-item {
  78.  
    margin-bottom: 25px;
  79.  
    position: relative;
  80.  
    }
  81.  
     
  82.  
    .login-wrapper > .layui-form > .layui-form-item:last-child {
  83.  
    margin-bottom: 0;
  84.  
    }
  85.  
     
  86.  
    .login-wrapper > .layui-form > .layui-form-item > .layui-input {
  87.  
    height: 46px;
  88.  
    line-height: 46px;
  89.  
    border-radius: 2px !important;
  90.  
    }
  91.  
     
  92.  
    .login-wrapper .layui-input-icon-group > .layui-input {
  93.  
    padding-left: 46px;
  94.  
    }
  95.  
     
  96.  
    .login-wrapper .layui-input-icon-group > .layui-icon {
  97.  
    width: 46px;
  98.  
    height: 46px;
  99.  
    line-height: 46px;
  100.  
    font-size: 20px;
  101.  
    color: #909399;
  102.  
    position: absolute;
  103.  
    left: 0;
  104.  
    top: 0;
  105.  
    text-align: center;
  106.  
    }
  107.  
     
  108.  
    .login-wrapper > .layui-form > .layui-form-item.login-captcha-group {
  109.  
    padding-right: 135px;
  110.  
    }
  111.  
     
  112.  
    .login-wrapper > .layui-form > .layui-form-item.login-captcha-group > .login-captcha {
  113.  
    height: 46px;
  114.  
    width: 120px;
  115.  
    cursor: pointer;
  116.  
    box-sizing: border-box;
  117.  
    border: 1px solid #e6e6e6;
  118.  
    border-radius: 2px !important;
  119.  
    position: absolute;
  120.  
    right: 0;
  121.  
    top: 0;
  122.  
    }
  123.  
     
  124.  
    .login-wrapper > .layui-form > .layui-form-item > .layui-form-checkbox {
  125.  
    margin: 0 !important;
  126.  
    padding-left: 25px;
  127.  
    }
  128.  
     
  129.  
    .login-wrapper > .layui-form > .layui-form-item > .layui-form-checkbox > .layui-icon {
  130.  
    width: 15px !important;
  131.  
    height: 15px !important;
  132.  
    }
  133.  
     
  134.  
    .login-wrapper > .layui-form .layui-btn-fluid {
  135.  
    height: 48px;
  136.  
    line-height: 48px;
  137.  
    font-size: 16px;
  138.  
    border-radius: 2px !important;
  139.  
    }
  140.  
     
  141.  
    .login-wrapper > .layui-form > .layui-form-item.login-oauth-group > a > .layui-icon {
  142.  
    font-size: 26px;
  143.  
    }
  144.  
     
  145.  
    .login-copyright {
  146.  
    color: #eee;
  147.  
    padding-bottom: 20px;
  148.  
    text-align: center;
  149.  
    position: relative;
  150.  
    z-index: 1;
  151.  
    }
  152.  
     
  153.  
    @media screen and (min-height: 550px) {
  154.  
    .login-wrapper {
  155.  
    margin: -250px auto 0;
  156.  
    position: absolute;
  157.  
    top: 50%;
  158.  
    left: 0;
  159.  
    right: 0;
  160.  
    width: 100%;
  161.  
    }
  162.  
     
  163.  
    .login-copyright {
  164.  
    position: absolute;
  165.  
    bottom: 0;
  166.  
    right: 0;
  167.  
    left: 0;
  168.  
    }
  169.  
    }
  170.  
     
  171.  
    .layui-btn {
  172.  
    background-color: #5FB878;
  173.  
    border-color: #5FB878;
  174.  
    }
  175.  
     
  176.  
    .layui-link {
  177.  
    color: #5FB878 !important;
  178.  
    }
  179.  
    </style>
  180.  
    </head>
  181.  
    <body>
  182.  
    <div class="login-wrapper layui-anim layui-anim-scale layui-hide">
  183.  
    <form class="layui-form">
  184.  
    <h2>登陆</h2>
  185.  
    <div class="layui-form-item layui-input-icon-group">
  186.  
    <i class="layui-icon layui-icon-username"></i>
  187.  
    <input class="layui-input" id="username" name="username" value="12345" placeholder="请输入登录账号" autocomplete="off"
  188.  
    lay-verType="tips" lay-verify="required" required/>
  189.  
    </div>
  190.  
    <div class="layui-form-item layui-input-icon-group">
  191.  
    <i class="layui-icon layui-icon-password"></i>
  192.  
    <input class="layui-input" id="pwd" name="pwd" value="12345" placeholder="请输入登录密码" type="password"
  193.  
    lay-verType="tips" lay-verify="required" required/>
  194.  
    </div>
  195.  
     
  196.  
    <!-- <div style="width: 200px;display:flex;">
  197.  
    <input type="text" name="captcha" placeholder="请输入验证码" maxlength="5" autocomplete="off" id="input_code">
  198.  
    <input type="button" id="code" onclick="createCode()" />
  199.  
    </div> -->
  200.  
    <div class="layui-form-item layui-input-icon-group">
  201.  
    <input type="text" placeholder="请输入验证码" maxlength="5" id="input_code">
  202.  
    <input type="button" id="code" onclick="createCode()" />
  203.  
    </div>
  204.  
     
  205.  
    <div class="layui-form-item">
  206.  
    <button class="layui-btn layui-bg-blue layui-btn-fluid " lay-filter="loginSubmit" lay-submit="">登录</button>
  207.  
    </div>
  208.  
     
  209.  
    </form>
  210.  
    </div>
  211.  
    <!-- <div class="login-copyright">copyright © 2020 航天科工系统仿真(北京)有限公司 all rights reserved.</div> -->
  212.  
    <script src="/static/assets/libs/layui/layui.js"></script>
  213.  
    {#<script src="{{ url_for('static',filename='plugin/layui/layui.js') }}"></script>#}
  214.  
    {#<script src="{{ url_for('static',filename='js/common.js') }}?v=318"></script>#}
  215.  
    <script>
  216.  
    //产生验证码
  217.  
    window.onload = function () {
  218.  
    createCode()
  219.  
    }
  220.  
    var code; //在全局定义验证码
  221.  
    function createCode() {
  222.  
    code = "";
  223.  
    var codeLength = 5; //验证码的长度
  224.  
    var checkCode = document.getElementById("code");
  225.  
    var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
  226.  
    'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); //随机数
  227.  
    for (var i = 0; i < codeLength; i ) { //循环操作
  228.  
    var index = Math.floor(Math.random() * 36); //取得随机数的索引(0~35)
  229.  
    code = random[index]; //根据索引取得随机数加到code上
  230.  
    }
  231.  
    checkCode.value = code; //把code值赋给验证码
  232.  
    }
  233.  
     
  234.  
    layui.use(['layer','form'],function () {
  235.  
    var $ = layui.jquery;
  236.  
    var layer = layui.layer;
  237.  
    var form = layui.form
  238.  
    $('.login-wrapper').removeClass('layui-hide');
  239.  
     
  240.  
     
  241.  
    //登陆事件
  242.  
    form.on('submit(loginSubmit)',function(data){
  243.  
    console.log('login click');
  244.  
    // 设置按钮文字 禁止点击
  245.  
    $(data.elem).attr('disabled',true).text('登陆中....');
  246.  
    console.log(JSON.stringify(data.field));
  247.  
    var inputCode = $('#input_code').val().toUpperCase(); //获取输入框内验证码并转化为大写
  248.  
    if(inputCode!=code){
  249.  
    layer.msg('验证码错误',{
  250.  
    icon:2,
  251.  
    anim:6
  252.  
    });
  253.  
    // 把按钮设置成初始可用的状态
  254.  
    setTimeout(function(){
  255.  
    $(data.elem).text("登录").removeAttr("disabled");
  256.  
    },1000);
  257.  
    }else{
  258.  
    // 网络请求
  259.  
    var loadIndex = layer.load(2);
  260.  
    $.ajax({
  261.  
    type:'post',
  262.  
    url:'/dologin',
  263.  
    data:JSON.stringify(data.field),
  264.  
    contentType:"application/json",
  265.  
    dataType:'json',
  266.  
    success:function(res){
  267.  
    console.log(res);
  268.  
    //关闭层
  269.  
    layer.close(loadIndex);
  270.  
    // 判断返回数据
  271.  
    if(res.code==0){
  272.  
    // 登录成功
  273.  
    $(data.elem).attr('disabled',true).text('登录成功');
  274.  
    // 使用layer的消息提示功能
  275.  
    layer.msg('登陆成功',{
  276.  
    icon:1,
  277.  
    time:1500
  278.  
    });
  279.  
    // 跳转到index页面
  280.  
    setTimeout(function(){
  281.  
    window.location.href = '/index';
  282.  
    },2000)
  283.  
    }else{
  284.  
    layer.msg(res.msg,{
  285.  
    icon:2,
  286.  
    anim:6
  287.  
    });
  288.  
    // 把按钮设置成初始可用的状态
  289.  
    setTimeout(function(){
  290.  
    $(data.elem).text("登录").removeAttr("disabled");
  291.  
    },1000);
  292.  
    }
  293.  
    },
  294.  
    })
  295.  
    }
  296.  
    return false;
  297.  
    })
  298.  
     
  299.  
    })
  300.  
    </script>
  301.  
    </body>
  302.  
    </html>
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfekgf
系列文章
更多 icon
同类精品
更多 icon
继续加载