使用jQuery如何写一个含验证码的登录界面


Posted in jQuery onMay 13, 2019

一个包含用户名,密码,验证码的简单的登陆界面,如下图所示:

使用jQuery如何写一个含验证码的登录界面

首先是 HTML 代码:

<div id="divID">
 <div style="background-color:transparent;">
  <form id="ff" method="post">
   <ul class="reg-box">
    <li>
     <label for="">账 号</label>
     <input type="text" name="accName" value="" class="account" maxlength="11" style="color:#999;" onBlur="textBlur(this)" onFocus="textFocus(this)"/>
     <span class="error error5"></span>
    </li>
    <li>
     <label for="">密 码</label>
     <input type="password" name="accPassWord" class="admin_pwd" value="" style="color:#999;" onBlur="textBlur(this)" onFocus="textFocus(this)"/>
     <span class="error error6"></span>
    </li>
    <li>
     <label for="">验证码</label>
     <input type="text" class="sradd photokey" id="key" value="" style="color:#999;ime-mode:disabled;-webkit-ime-mode:inactive;" onBlur="textBlur(this)" onFocus=" textFocus(this) " />
     <span class="add phoKey"></span>
     <span class="error error7"></span>
    </li>
   </ul>
   <div class="sub">
    <input type="submit" value="立即登录"/>
   </div>
  </form>
 </div>
</div>

然后是实现验证用户的输入是否正确的 JS 代码:

//文本框默认提示文字
function textFocus(el) {
 if (el.defaultValue == el.value) { el.value = ''; el.style.color = '#333'; }
}
function textBlur(el) {
 if (el.value == '') { el.value = el.defaultValue; el.style.color = '#999'; }
}

$(function(){
 /*生成验证码*/
 create_code();

 //登录页面的提示文字
 //账户输入框失去焦点
 (function login_validate(){
  $(".reg-box .account").blur(function(){
   //reg=/^1[3|4|5|8][0-9]\d{4,8}$/i;//验证手机正则(输入前7位至11位)

   if( $(this).val()==""|| $(this).val()=="请输入您的账号")
   {
    $(this).addClass("errorC");
    $(this).next().html("账号不能为空!");
    $(this).next().css("display","block");
    $(".sub input").prop('disabled', true);
   }
//    else if($(".reg-box .account").val().length<11)
//    {
//     $(this).addClass("errorC");
//     $(this).next().html("账号长度有误!");
//     $(this).next().css("display","block");
//    }
//    else if(!reg.test($(".reg-box .account").val()))
//    {
//     $(this).addClass("errorC");
//     $(this).next().html("账号不存在!");
//     $(this).next().css("display","block");
//    }
   else
   {
    $(".sub input").prop('disabled', false);
    $(this).addClass("checkedN");
    $(this).removeClass("errorC");
    $(this).next().empty();
   }
  });
  /*密码输入框失去焦点*/
  $(".reg-box .admin_pwd").blur(function(){
   //reg=/^[\@A-Za-z0-9\!\#\$\%\^\&\*\.\~]{6,22}$/;

   if($(this).val() == ""){
    $(this).addClass("errorC");
    $(this).next().html("密码不能为空!");
    $(this).next().css("display","block");
    $(".sub input").prop('disabled', true);
   }
//    else if(!reg.test($(".admin_pwd").val())) {
//     $(this).addClass("errorC");
//     $(this).next().html("密码为6~12位的数字、字母或特殊字符!");
//     $(this).next().css("display","block");
//    }
   else {
    $(".sub input").prop('disabled', false);
    $(this).addClass("checkedN");
    $(this).removeClass("errorC");
    $(this).next().empty();
   }
  });

  /*验证码输入框失去焦点*/
  $(".reg-box .photokey").blur(function(){
   var code1=$('.reg-box input.photokey').val().toLowerCase();
   var code2=$(".reg-box .phoKey").text().toLowerCase();
   if(code1!=code2)
   {
    $(this).addClass("errorC");
    $(this).next().next().html("验证码输入错误!!!");
    $(this).next().next().css("display","block");
    $(".sub input").prop('disabled', true);
   }
   else
   {
    $(".sub input").prop('disabled', false);
    $(this).removeClass("errorC");
    $(this).next().next().empty();
    $(this).addClass("checkedN");
   }
  })
 })();
});

函数 create_code() 用于生成验证码:

function create_code() {
 function shuffle() {
  var arr = ['1', 'r', 'Q', '4', 'S', '6', 'w', 'u', 'D', 'I', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
   'q', '2', 's', 't', '8', 'v', '7', 'x', 'y', 'z', 'A', 'B', 'C', '9', 'E', 'F', 'G', 'H', '0', 'J', 'K', 'L', 'M', 'N', 'O', 'P', '3', 'R',
   '5', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
  return arr.sort(function () {
   return (Math.random() - .5);
  });
 };
 shuffle();
 function show_code() {
  var ar1 = '';
  var code = shuffle();
  for (var i = 0; i < 6; i++) {
   ar1 += code[i];
  }
  ;
  //var ar=ar1.join('');
  $(".reg-box .phoKey").text(ar1);
 };
 show_code();
 $(".reg-box .phoKey").click(function () {
  show_code();
 });
}

最后是非常重要的 CSS 代码:

body{
 background: #000;
}
#divID {
  position: fixed;
  left: 47%;
  top: 53%;
  width: 500px;
  margin-left: -200px;
  margin-top: -150px;
  font-family: "黑体";
  /*禁止复制粘贴*/
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select:none;
  color:#fff;
 }
 .register_dialog_info {
  float: left;
  margin-left:10px;
  color: #fff;
  margin-top: 5px;
  font-size: 20px;
 }
 form{padding: 20px 0px;}
 ul li {list-style: none;}
 .sub {
  text-align: center;
 }
 .sub input {
  display: inline-block;
  width: 300px;
  background-color: #012246;
  color: rgb(255, 255, 255);
  font-size: 20px;
  text-align: center;
  height: 40px;
  line-height: 40px;
  font-family: 黑体;
  outline: none;
  border: none;
  margin: auto;
  border-radius: 10px;
 }
 input[type = "submit"]:hover{cursor: pointer;}

 .reg-box { padding-left: 30px; }

 .reg-box li { line-height: 44px; width: 500px; overflow: hidden; }

 .reg-box li label { width: 68px; height: 50px; float: left; line-height: 50px; text-align: right; padding-right: 20px; }

 .reg-box li input,.reg-box li select{ border-radius: 3px; padding: 6px 0; font-size: 16px; width: 296px; height: 49px; line-height: 28px; border: 1px solid #dddddd; text-indent: 0.5em; float: left; }

 .reg-box li select option{font-size:16px;}

 /*验证码*/
 .add { width: 128px; height: 44px; float: left; _display: inline; cursor: pointer; margin-left: 20px; }

 .reg-box li .sradd { width: 148px; text-indent: 4px; font-size: 14px; }

 .reg-box li .input-code { width: 106px; padding: 10px; font-family: Arial; font-style: italic; color: red; letter-spacing: 1px; cursor: pointer; text-align: center; text-indent: 0; }

 .yzm,.phoKey { background: #012246; text-align: center; line-height: 44px; color: #fff; border-radius: 3px;}

 .phoKey{letter-spacing: 3px; font-size:18px;}

 .yzmc { background: #dddddd; text-align: center; line-height: 44px; color: #999; }

 .error { clear:both;display:block;color: red; padding-left: 90px; padding-bottom:5px;height:20px;float: left; font-size:12px;line-height: 20px;}

 input { background-color: #fff; outline: none; }

 .reg-box li { width: auto; }

 .reg-box li input.errorC, .errorC{ border: 1px solid blue; }

 .reg-box li input.checkedN , .checkedN{ border: 1px solid #1ece6d; }

以上所述是小编给大家介绍的如何写一个含验证码的登录界面详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

jQuery 相关文章推荐
如何编写jquery插件
Mar 29 jQuery
jquery中$.fn和图片滚动效果实现的必备知识总结
Apr 21 jQuery
jQuery使用ajax_动力节点Java学院整理
Jul 05 jQuery
jQuery实现可编辑表格并生成json结果(实例代码)
Jul 19 jQuery
jQuery Ajax向服务端传递数组参数值的实例代码
Sep 03 jQuery
jQuery判断网页是否已经滚动到浏览器底部的实现方法
Oct 27 jQuery
详解使用jQuery.i18n.properties实现js国际化
May 04 jQuery
JavaScript自动生成 年月范围 选择功能完整示例【基于jQuery插件】
Sep 03 jQuery
js判断复选框是否选中的方法示例【基于jQuery】
Oct 10 jQuery
jQuery实现聊天对话框
Feb 08 jQuery
jQuery带控制按钮轮播图插件
Jul 31 jQuery
详解jQuery的核心函数和事件处理
Feb 18 jQuery
JQuery特殊效果和链式调用操作示例
May 13 #jQuery
JQuery的加载和选择器用法简单示例
May 13 #jQuery
JQuery事件委托原理与用法实例分析
May 13 #jQuery
jQuery事件绑定和解绑、事件冒泡与阻止事件冒泡及弹出应用示例
May 13 #jQuery
使vue实现jQuery调用的两种方法
May 12 #jQuery
jquery3和layui冲突导致使用layui.layer.full弹出全屏iframe窗口时高度152px问题
May 12 #jQuery
jQuery实现动态生成年月日级联下拉列表示例
May 11 #jQuery
You might like
利用PHP实现图片等比例放大和缩小的方法详解
2013/06/06 PHP
Yii基于数组和对象的Model查询技巧实例详解
2015/12/28 PHP
PHP lcfirst()函数定义与用法
2019/03/08 PHP
PHP Swoole异步Redis客户端实现方法示例
2019/10/24 PHP
[转]JS宝典学习笔记
2007/02/07 Javascript
类之Prototype.js学习
2007/06/13 Javascript
JS控制文本框textarea输入字数限制的方法
2013/06/17 Javascript
jQuery实现单击按钮遮罩弹出对话框(仿天猫的删除对话框)
2014/04/10 Javascript
JS利用cookie记忆当前位置的防刷新导航效果
2015/10/15 Javascript
获取阴历(农历)和当前日期的js代码
2016/02/15 Javascript
通过Tabs方法基于easyUI+bootstrap制作工作站
2016/03/28 Javascript
JavaScript页面实时显示当前时间实例代码
2016/10/23 Javascript
Bootstrap3下拉菜单的实现
2017/02/22 Javascript
深入理解vue Render函数
2017/07/19 Javascript
vue实现手机号码抽奖上下滚动动画示例
2017/10/18 Javascript
vue-cli脚手架config目录下index.js配置文件的方法
2018/03/13 Javascript
微信JSSDK实现打开摄像头拍照再将相片保存到服务器
2019/11/15 Javascript
JavaScript定时器使用方法详解
2020/03/26 Javascript
vue父子组件间引用之$parent、$children
2020/05/20 Javascript
Taro小程序自定义顶部导航栏功能的实现
2020/12/17 Javascript
[47:36]Optic vs Newbee 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/18 DOTA
python实现根据ip地址反向查找主机名称的方法
2015/04/29 Python
python 类详解及简单实例
2017/03/24 Python
Python基于property实现类的特性操作示例
2018/06/15 Python
Python使用Tkinter实现滚动抽奖器效果
2020/01/06 Python
tensorflow 限制显存大小的实现
2020/02/03 Python
Python自定义聚合函数merge与transform区别详解
2020/05/26 Python
使用HTML5加载音频和视频的实现代码
2020/11/30 HTML / CSS
idealfit英国:世界领先的女性健身用品和运动衣物品牌
2017/11/25 全球购物
BLACKMORES澳洲官网:澳大利亚排名第一的保健品牌
2018/09/27 全球购物
哈萨克斯坦最大的时装、鞋子和配饰在线商店:Lamoda.kz
2019/11/19 全球购物
战友聚会邀请函
2014/01/18 职场文书
民警个人对照检查剖析材料
2014/09/17 职场文书
2016领导干部廉洁自律心得体会
2016/01/13 职场文书
Pytest之测试命名规则的使用
2021/04/16 Python
python中validators库的使用方法详解
2022/09/23 Python