当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net+Ajax校验用户是否存在的实现代码

asp.net+Ajax校验用户是否存在的实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

2013年属虎人的运程,鱼翅怎么发,5252bbb

需求:做一个ajax登录

主要技术点:jquery ajax以及blur事件

当用户名输入框失去焦点的时候就会触发blur事件,然后进行ajax请求,获得结果(true或者false),如果请求结果为true,就把用户名输入框图片替换成ok,并且输出文字:恭喜您, 这个帐号可以注册,否则就替换成图片no,并且输出文字:账号已存在

源代码:

前台:
复制代码 代码如下:

<%@ page language="c#" masterpagefile="~/top_down.master" autoeventwireup="true" codefile="registermember.aspx.cs"inherits="member_registermember" title="注册用户" %>
<asp:content id="content1" contentplaceholderid="head" runat="server">
<link href="../admin/css/template.css" rel="stylesheet" type="text/css" />
<link href="../admin/css/validationengine.jquery.css" rel="stylesheet" type="text/css" />
<script src="../admin/scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../js/jquery.validationengine.js" type="text/javascript"></script>
<script src="../admin/scripts/isvalid.js" type="text/javascript"></script>
<script src="../js/languages/jquery.validationengine-zh_cn.js" type="text/javascript"></script>
<script type="text/javascript">
var ischeck=false;
$(function(){
// binds form submission and fields to the validation engine
$("#form1").validationengine();
//当鼠标失去焦点的时候验证
$("#txtusername").blur(function(){
$.ajax({
url:"data/getmemberinfo.ashx?method=checkexistusername",
data:{"username":$("#txtusername").val()},
type:"post",
success:function(text){
$("#tduser").empty();//清空内容
var item;
if(text=="true"){
item='<img src="../images/ok.png"/>恭喜您,这个帐号可以注册!';
ischeck=true;
}
else
item='<img src="../images/no.png"/>对不起,这个帐号已经有人注册了!';
$("#tduser").append(item);
}
});
});
});
function checkform1()
{
if(ischeck)
{
form1.submit();
}
else{
alert("请验证用户名");
}
}
</script>
</asp:content>
<asp:content id="content2" contentplaceholderid="contentplaceholder1" runat="server">
<form id="form1" action="data/getmemberinfo.ashx?method=savememberinfo" method="post">
<div class="content">
<div class="left_side">
<div class="logo_bottom"></div>
</div>
<div class="right_side zhuce">
<div class="zhuce_title"><p class="hide">注册新用户</p></div>
<div class="zhuce_p">
<table width="578" class="zc_table1">
<tr>
<td width="93" class="zc_tar">用户名:</td>
<td width="200" class="zc_tal"><input type="text" class="zc_input1 validate[required,custom[loginname]] text-input"name="txtusername" id="txtusername"/><!--loginname-->
</td>
<td width="269" class="zc_font" id="tduser"></td>
</tr>
<tr>
<td class="zc_tar">密码:</td>
<td class="zc_tal"><input type="password" class="zc_input2 validate[required,custom[loginpwd]] text-input" id="txtpwd"name="txtpwd"/></td>
<td class="zc_font"></td>
</tr>
<tr>
<td class="zc_tar">确认密码:</td>
<td class="zc_tal"><input type="password" class="zc_input3 validate[required,equals[txtpwd] text-input" /></td>
<td class="zc_font"></td>
</tr>
<tr>
<td class="zc_tar">e-mail:</td>
<td class="zc_tal"><input type="text" class="zc_input4 validate[required,custom[email] text-input" name="txtemail"id="txtemail"/></td>
<td class="zc_font"></td>
</tr>
<tr>
<td class="zc_tar">验证码:</td>
<td class="zc_tal" colspan="2"><input type="text" class="zc_input5" name="txtcheckcode" id="txtcheckcode"/><imgsrc="../admin/filemanage/verifychars.ashx" alt="验证码" /></td>
</tr>
<tr><td> </td></tr>
<tr>
<td colspan="3" align="center"><a href="javascript:checkform1()"><img src="../images/zhuce_sumbit.png" /></a></td>
</tr>
</table>
</div>
</div>
</div>
</form>
</asp:content>

后台事件:
复制代码 代码如下:

<%@ webhandler language="c#" class="getmemberinfo" %>
using system;
using system.web;
using common;
using czcraft.model;
using czcraft.bll;
using system.web.sessionstate;
public class getmemberinfo : ihttphandler,irequiressessionstate
{
// //记录日志
private static readonly log4net.ilog logger =log4net.logmanager.getlogger(system.reflection.methodbase.getcurrentmethod().declaringtype);
public void processrequest(httpcontext context)
{
string methodname = context.request["method"];
if (!string.isnullorempty(methodname))
callmethod(methodname, context);
}
/// <summary>
/// 根据业务需求调用不同的方法
/// </summary>
/// <param name="method">方法</param>
/// <param name="context">上下文</param>
public void callmethod(string method, httpcontext context)
{
switch (method)
{
case "checkexistusername":
checkexistusername(context);
break;
//case "searchmember":
// searchmember(context);
// break;
case "savememberinfo":
savememberinfo(context);
break;
//case "removemember":
// removemember(context);
// break;
//case "getmember":
// getmember(context);
// break;
default:
return;
}
}
/// <summary>
/// 验证帐号是否存在
/// </summary>
/// <param name="context"></param>
public void checkexistusername(httpcontext context)
{
string username = context.request["username"];
if (tools.isvalidinput(ref username, true))
{
context.response.write(new memberbll().checkexistusername(username));
}
}
/// <summary>
/// 保存用户信息
/// </summary>
/// <param name="context"></param>
public void savememberinfo(httpcontext context)
{
try
{
//表单读取
string txtusername = context.request["txtusername"];
string txtpwd = context.request["txtpwd"];
string txtemail = context.request["txtemail"];
string txtcheckcode = context.request["txtcheckcode"];
//验证码校验
if (!txtcheckcode.equals(context.session["checkcode"].tostring()))
{
return;
}
//字符串sql注入检测
if (tools.isvalidinput(ref txtusername, true) && tools.isvalidinput(ref txtpwd, true) && tools.isvalidinput(ref txtemail, true))
{
member info = new member();
info.username = txtusername;
info.password = txtpwd;
info.email = txtemail;
info.states = "0";
if (new memberbll().addnew(info) > 0)
{
smtp smtp = new smtp(info.email);
string webpath = context.request.url.scheme + "://" + context.request.url.authority + "/default.aspx";
smtp.activation(webpath, info.username);//发送激活邮件
jscript.alertandredirect("注册用户成功!!", "../default.aspx");
}
else {
jscript.alertandredirect("注册用户失败!", "../default.aspx");
}
}
}
catch (exception ex)
{
logger.error("错误!", ex);
}
}
public bool isreusable {
get {
return false;
}
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网