您的位置:首页 > 其它

ajax 验证用户名是否唯一

2012-03-28 12:18 274 查看
昨天我一哥们问我如何ajax验证用户名唯一性的问题,这个问题虽然简单,但是对于新手没有经验的童鞋们来讲,还是不清楚,这里呢,我写了一个例子,例子不难,用asp.Net技术+ajax+handler即可,另外,数据传递通过json格式,具体的我讲不清楚了,呵呵,我把我的例子放在这里,有需要的童鞋可以拿去,在下去也~~~

一般处理程序代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ValidateUserName
{
/// <summary>
/// ValidateUserInfo 的摘要说明
/// </summary>
public class ValidateUserInfo : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string fun = context.Request["fun"].Trim();
switch (fun)
{
case "name":
ValidateUserName(context);
break;
}
}

/// <summary>
/// 验证用户名是否存在
/// </summary>
/// <param name="context"></param>
private void ValidateUserName(HttpContext context)
{
//ajax通过一般处理程序提交的请求  在其中拿到用户输入的用户名
string name = context.Request["name"].Trim();
//从数据库中验证是否存在,这里省略
if (name != "zzz" && name != "xxx" && name != "aaa")
{
//如果验证不存在,返回页面一个False
context.Response.Write("False");
}
else
{
//反之返回一个True
context.Response.Write("True");
}
}

public bool IsReusable
{
get
{
return false;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: