本程序主要功能是检查用户输入的用户名是否在检查的范围之内
CheckUserName.htm代码:
<head> <title></title> <script src="jquery-1.8.3.js" type="text/javascript"></script> <script src="MyAjax.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("#username").blur(function () { var username = $("#username").val(); MyAjax("CheckUserName.ashx?username=" + username, function (resTxt) { //resTxt为response返回的结果 if (resTxt == "ok") { $("#msg").text("用户名可用"); } else { var strs = resTxt.split("|"); //对response返回的结果进行分割 $("#msg").text(strs[1]); } }, function (status) { $("#msg").text("服务器出现" + status + "错误"); }); }); }); </script> </head> <body> <input type="text" id="username" /><span id="msg"></span> </body>
CheckUserName.ashx代码:
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; string username = context.Request["username"]; if (string.IsNullOrEmpty(username)) { context.Response.Write("error|用户名必填"); return; } if (username.Length < 3 || username.Length >= 10) { context.Response.Write("error|用户名必须在3-10之间"); return; } if (username.Contains("毒") || username.Contains("枪")) { context.Response.Write("error|含有违禁词汇"); return; } int r = (int)SQLHelper.ExecuteScalar("select count(*) from User where name=@name", new SqlParameter { ParameterName="@name",Value=username}); if (r > 0) { context.Response.Write("error|用户名已被使用"); return; } context.Response.Write("ok");