JQuery用户登录时的检测
来源:互联网 发布:浙江2017年专升本数据 编辑:程序博客网 时间:2024/06/06 00:09
一、在web开发中经常会遇到用户登录进行检测
var z_user=0,z_psword=0,z_code=0;var code_reg = new RegExp("^[\\da-z]{4}$", "i");var pass_reg = new RegExp("^.{6,}$", "i");var user_reg = new RegExp("^[\\d\\.a-z_A-Z]{4,20}|[\u4e00-\u9fa5]$", "i");$('#user').focus(function() {if($(this).val() == '') { $('#r_name').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入用户名!</p>'); z_user = 0; }}).keyup(function() { if($(this).val() == ''){ $('#r_name').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入用户名!</p>'); z_user = 0; }else if (!user_reg.test($(this).val())){ $('#r_name').html('<p style="color:#FE6201"><img style="margin-right:2px;" src="/Style/M/images/zhuce2.gif">您输入的用户名格式不正确!</p>'); z_user = 0; }else{ $('#r_name').html('<p style="color:#A6E217"><img style="margin-right:2px;" src="/Style/M/images/zhuce3.gif">您输入的用户名格式正确!</p>'); z_user = 1; } }).blur(function() { if($(this).val() == ''){ $('#r_name').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入用户名!</p>'); z_user = 0; }else if (!user_reg.test($(this).val())){ $('#r_name').html('<p style="color:#FE6201"><img style="margin-right:2px;" src="/Style/M/images/zhuce2.gif">您输入的用户名格式不正确!</p>'); z_user = 0; }else { $('#r_name').html('<p style="color:#A6E217"><img style="margin-right:2px;" src="/Style/M/images/zhuce3.gif">您输入的用户名格式正确!</p>'); z_user = 1; }}); $('#psword').focus(function() {if($(this).val() == '') { $('#r_psword').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入密码!</p>'); z_psword = 0; }}).keyup(function() { if($(this).val() == ''){ $('#r_psword').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入密码!</p>'); z_psword = 0; }else if (!pass_reg.test($(this).val())){ $('#r_psword').html('<p style="color:#FE6201"><img style="margin-right:2px;" src="/Style/M/images/zhuce2.gif">您输入的密码格式不正确!</p>'); z_psword = 0; }else { $('#r_psword').html('<p style="color:#A6E217"><img style="margin-right:2px;" src="/Style/M/images/zhuce3.gif">您输入的密码格式正确!</p>'); z_psword = 1; } }).blur(function() { if($(this).val() == ''){ $('#r_psword').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入密码!</p>'); z_psword = 0; }else if (!pass_reg.test($(this).val())){ $('#r_psword').html('<p style="color:#FE6201"><img style="margin-right:2px;" src="/Style/M/images/zhuce2.gif">您输入的密码格式不正确!</p>'); z_psword = 0; }else { $('#r_psword').html('<p style="color:#A6E217"><img style="margin-right:2px;" src="/Style/M/images/zhuce3.gif">您输入的密码格式正确!</p>'); z_psword = 1; }}); $('#psvercode').focus(function() {if($(this).val() == '') { $('#r_psvercode').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入验证码!</p>'); z_code = 0; }}).keyup(function() { if($(this).val() == ''){ $('#r_psvercode').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入验证码!</p>'); z_code = 0; }else if (!code_reg.test($(this).val())){ $('#r_psvercode').html('<p style="color:#FE6201"><img style="margin-right:2px;" src="/Style/M/images/zhuce2.gif">验证码格式不正确!</p>'); z_code = 0; }else { $('#r_psvercode').html('<p style="color:#A6E217"><img style="margin-right:2px;" src="/Style/M/images/zhuce3.gif">验证码格式正确!</p>'); z_code = 1; } }).blur(function() { if($(this).val() == ''){ $('#r_psvercode').html('<p style="color:#2FA7F8"><img style="margin-right:2px;" src="/Style/M/images/zhuce1.gif">请输入验证码!</p>'); z_code = 0; }else if (!code_reg.test($(this).val())){ $('#r_psvercode').html('<p style="color:#FE6201"><img style="margin-right:2px;" src="/Style/M/images/zhuce2.gif">验证码格式不正确!</p>'); z_code = 0; }else { $('#r_psvercode').html('<p style="color:#A6E217"><img style="margin-right:2px;" src="/Style/M/images/zhuce3.gif">验证码格式正确!</p>'); z_code = 1; }});function login(){var sUserName = $('#user').val();var sPassword = $('#psword').val();var sVerCode = $('#psvercode').val();if (z_user == 1 && z_psword == 1 && z_code == 1) {$.ajax({ url:'*******', type:'post', data:{ 'sUserName':sUserName, 'sPassword':sPassword, 'sVerCode':sVerCode }, async : false, //默认为true 异步 dataType: "json", error:function(){ layer.msg('error'); }, success: function (d, s, r) {if(d){if(d.status == 0){layer.msg(d.message,{icon: 2});}else{ var url = "****";window.location.href = url;}}} });}}
二、html代码
<div class="row"><label class="control-label col-md-3 text-right" for="user" style="margin-top: 8px;">用户名</label><div class="col-md-9"><input type="text" id="user" class="form-control" placeholder="用户名"/><div id="r_name" style="float: left;font-size: 12px;color: #ff0000;height: 22px;line-height: 22px;"></div></div></div><div class="row"><label class="control-label col-md-3 text-right" for="psword" style="margin-top: 8px;">密 码</label><div class="col-md-9"><input type="password" id="psword" class="form-control" placeholder="请输入密码"/><p id="r_psword"> </p></div></div><div class="row"><label class="control-label col-md-3 text-right" for="psvercode" style="margin-top: 8px;">验证码</label><div class="col-md-5"><input type="text" id="psvercode" class="form-control" placeholder="请输入验证码" /><div id="r_psvercode" style="float: left;font-size: 12px;color: #ff0000;height: 22px;line-height: 22px;"></div></div><div class="col-md-3"><img src="__URL__/verify" onclick="this.src=this.src+'?t='+Math.random()" id="imVcode" alt="点击换一个校验码" style=" width: 115px; height:36px;border: 1px solid #ccc;"/></div></div><div class="row"><button class="btn btn-info col-md-10 col-md-offset-1" onclick="login();">登录</button></div>
注意此验证引用了JQuery和layer弹出框控件
1 0
- JQuery用户登录时的检测
- jQuery-Ajax用户登录功能的实现
- jQuery练习(用户登录)
- drupal7 检测用户是否登录
- MVC 用户登录状态检测
- thinkphp5检测用户登录状态
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- jquery与php交互的ajax应用第一课:检测用户注册时用户名是否存在
- jQuery+Ajax实现用户登录
- jQuery和CSS3炫酷GOOGLE样式的用户登录界面
- MVC 检测用户是否已经登录
- ajax实现检测用户是否登录
- 用户登录邮箱检测表单提交
- 使用Jquery搭建最佳用户体验的登录页面 之 记住密码自动登录功能
- cookie:记住用户登录时的用户名
- cookie:记住用户登录时的用户名
- 简单的用户登录
- 黑马程序员——IO流篇
- IOS实现高斯模糊
- leetcode: Summary Ranges
- 一张图看尽 Linux 内核运行原理
- IOS实现背景色渐变
- JQuery用户登录时的检测
- javascript js undefined null 0 false void(0) NaN 空字符串 比较 区别
- 制作OpenStack Windows Server 2012镜像
- centos安装mysql以及mysql++开发环境
- 蓝桥杯 动态数组使用
- Java 8会干掉Scala吗?
- unix高级编程时遇到apue.h 报错问题
- Android材料设计第一篇
- H5、React Native、Native应用对比分析