asp.net +Ajax an implementation code that verifies the existence of the user

  • 2020-05-16 06:54:57
  • OfStack

Requirement: do 1 ajax login

Main technical points :jquery ajax and blur events

When user name input box loses focus will trigger blur events, then ajax requests, get results (true or false), if the request for true result, the user name input box picture replace ok, and output text: congratulations, the account can be registered, otherwise will replace no images, and the output text: account already exists

The source code:

Front desk:
 
<%@ Page Language="C#" MasterPageFile="~/Top_Down.master" AutoEventWireup="true" CodeFile="RegisterMember.aspx.cs"Inherits="Member_RegisterMember" Title=" Registered users " %> 
<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(); 
// Verify when the mouse loses focus  
$("#txtUserName").blur(function(){ 
$.ajax({ 
url:"Data/GetMemberInfo.ashx?method=CheckExistUserName", 
data:{"username":$("#txtUserName").val()}, 
type:"post", 
success:function(text){ 
$("#tdUser").empty();// Empty content  
var item; 
if(text=="True"){ 
item='<img src="../images/ok.png"/> Congratulations to you , This account can be registered !'; 
IsCheck=true; 
} 
else 
item='<img src="../images/no.png"/> I'm sorry , The account has been registered !'; 
$("#tdUser").append(item); 
} 
}); 
}); 
}); 
function CheckForm1() 
{ 
if(IsCheck) 
{ 
form1.submit(); 
} 
else{ 
alert(" Please verify the user name "); 
} 
} 
</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"> Register a new user </p></div> 
<div class="zhuce_p"> 
<table width="578" class="zc_table1"> 
<tr> 
<td width="93" class="zc_tar"> User name: </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"> Password: </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"> Confirm password: </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"> Verification code: </td> 
<td class="zc_tal" colspan="2"><input type="text" class="zc_input5" name="txtCheckCode" id="txtCheckCode"/><imgsrc="../Admin/FileManage/VerifyChars.ashx" alt=" Verification code " /></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> 

Background events:
 
<%@ 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 
{ 
// // log  
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> 
///  Call different methods based on business requirements  
/// </summary> 
/// <param name="Method"> methods </param> 
/// <param name="context"> 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> 
///  Verify that the account exists  
/// </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> 
///  Save user information  
/// </summary> 
/// <param name="context"></param> 
public void SaveMemberInfo(HttpContext context) 
{ 
try 
{ 
// Read the form  
string txtUserName = context.Request["txtUserName"]; 
string txtPwd = context.Request["txtPwd"]; 
string txtEmail = context.Request["txtEmail"]; 
string txtCheckCode = context.Request["txtCheckCode"]; 
// Verification code check  
if (!txtCheckCode.Equals(context.Session["checkcode"].ToString())) 
{ 
return; 
} 
// string sql Injection test  
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);// Send an activation email  
JScript.AlertAndRedirect(" Registered user successfully !!", "../Default.aspx"); 
} 
else { 
JScript.AlertAndRedirect(" Registered user failed !", "../Default.aspx"); 
} 
} 
} 
catch (Exception ex) 
{ 
logger.Error(" error !", ex); 
} 
} 
public bool IsReusable { 
get { 
return false; 
} 
} 
} 

Related articles: