后台CS页面 用户名是否存在自写数据库查询方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
namespace xh.shop.web
{
public partial class reg2 : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e)
{
}
protected void Button4_Click( object sender, EventArgs e)
{
}
// 重置
protected void Button5_Click( object sender, EventArgs e)
{
txtanswer.Text = "" ;
txtemail.Text = "" ;
txtname.Text = "" ;
txtpwd.Text = "" ;
txtqrpwd.Text = "" ;
txtquestion.Text = "" ;
}
// 返回
protected void Button6_Click( object sender, EventArgs e)
{
Response.Redirect( " index.aspx " );
}
// 注册
protected void butcz( object sender, EventArgs e)
{
// ----------------------------------------------------验证开始
string answer = txtanswer.Text.Trim();
string email = txtemail.Text.Trim();
string name = txtname.Text.Trim();
string pwd = txtpwd.Text.Trim();
string qrpwd = txtqrpwd.Text.Trim();
string question = txtquestion.Text.Trim();
// 验证输入是否完整
if (answer.Length == 0 || email.Length == 0 || name.Length == 0 || pwd.Length == 0 || qrpwd.Length == 0 || question.Length == 0 )
{
Response.Write( " <script language='JavaScript'>alert('输入不完整')</script> " );
return ;
}
// 验证密码与确认密码是否一致
if (pwd != qrpwd)
{
Response.Write( " <script language='JavaScript'>alert('密码与确认密码不一致')</script> " );
return ;
}
// 验证密码
if ( ! Regex.IsMatch(pwd, " ^[a-zA-Z0-9]{6,20}$ " ))
{
Response.Write( " <script language='JavaScript'>alert(' 密码长度必须大于6个字符小于20个字符,只能为英语字、数字,例如:snsn2003等')</script> " );
return ;
}
// 验证电子邮件
if ( ! Regex.IsMatch(email, @" \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* " ))
{
Response.Write( " <script language='JavaScript'>alert('请输入有效的邮件地址')</script> " );
return ;
}
// 验证用户名是否存在
if ( new xh.shop.DAL.user().Exists(name))
{
Response.Write( " <script language='JavaScript'>alert('用户名已经存在')</script> " );
return ;
}
// --------------------------------------------------------验证结束
int res = new xh.shop.DAL.user().Add( new xh.shop.Model.user() {
username = name,
createdate = DateTime.Now,
answer = answer,
email = email,
password = pwd,
question = question
});
if (res > 0 )
{
Response.Write( " <script language='JavaScript'>alert('添加成功')</script> " );
txtanswer.Text = "" ;
txtemail.Text = "" ;
txtname.Text = "" ;
txtpwd.Text = "" ;
txtqrpwd.Text = "" ;
txtquestion.Text = "" ;
}
else
{
Response.Write( " <script language='JavaScript'>alert('添加失败')</script> " );
return ;
}
}
}
}
前台页面 images文件夹下用到了gou.gif显示输入正确 jqur1.4.2
<% @ Page Title = "" Language = " C# " MasterPageFile = " ~/topfoot.Master " AutoEventWireup = " true "
CodeBehind = " reg2.aspx.cs " Inherits = " xh.shop.web.reg2 " %>
< asp:Content ID ="Content1" ContentPlaceHolderID ="head" runat ="server" >
< script src ="js/jquery-1.4.2.js" type ="text/javascript" ></ script >
< script type ="text/javascript" >
function checkform() {
var name = $.trim($( ' #<%=txtname.ClientID %> ' ).val());
var email = $.trim($( ' #<%=txtemail.ClientID %> ' ).val());
var pwd = $.trim($( ' #<%=txtpwd.ClientID %> ' ).val());
var qrpwd = $.trim($( ' #<%=txtqrpwd.ClientID %> ' ).val());
var question = $.trim($( ' #<%=txtquestion.ClientID %> ' ).val());
var answer = $.trim($( ' #<%=txtanswer.ClientID %> ' ).val());
// 验证输入是否完整
if (answer.length == 0 || email.length == 0 || name.length == 0 || pwd.length == 0 || qrpwd.length == 0 || question.length == 0 ) {
alert( " 输入不完整 " );
return false ;
}
// 验证密码与确认密码是否一致
if (pwd != qrpwd) {
alert( " 密码与确认密码不一致 " );
return false ;
}
// 验证密码 这里的正则表达式的判断方法跟c#不一样 请注意:/正则表达式 /g g表示全部查找
var reg = / ^[a-zA-Z0-9]{6,20}$ / g;
if ( ! reg.test(pwd)) {
alert( " 密码长度必须大于6个字符小于20个字符,只能为英语字、数字,例如:snsn2003等 " );
return false ;
}
// 验证电子邮件
reg = / \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* / g;
if ( ! reg.test(email)) {
alert( " 请输入有效的邮件地址 " );
return false ;
}
// 如果都正确 则返回真
return true ;
}
// 验证用户名失去焦点时是否存在用户名
function checkname(username) {
username = $.trim(username);
if (username.length == 0 ) {
$( " #meusername " ).html( " <span style='color:red;'>用户名不能为空</span> " );
return ;
}
else {
var url = " handler/checkusername.ashx?username= " + username + " &t= " + new Date().valueOf();
$.get(url, function (data) {
if (data == " false " ) {
$( " #meusername " ).html( " <span style='color:red;'>用户名已经存在</span> " );
}
else {
$( " #meusername " ).html( " <img src=images/gou.gif></a> " );
}
})
}
}
// 验证电子邮件失去焦点时是否合法
function checkemail(email) {
if (email.length == 0 ) {
$( " #mecemail " ).html( " <span style='color:red;'>电子邮件不能为空</span> " );
return ;
}
else {
reg = / \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* / g;
if ( ! reg.test(email)) {
$( " #mecemail " ).html( " <span style='color:red;'>请输入有效的邮件地址</span> " );
return ;
}
else {
$( " #mecemail " ).html( " <img src=images/gou.gif></a> " );
}
}
}
// 验证密码确认是否一致
function checkqrpwd(qrpwd) {
var pwd = $.trim($( ' #<%=txtpwd.ClientID %> ' ).val());
if (pwd.length != 0 ) {
if (qrpwd != pwd) {
$( " #mecqrpwd " ).html( " <span style='color:red;'>确认密码不一致</span> " );
return ;
}
else {
$( " #mecqrpwd " ).html( " <img src=images/gou.gif></a> " );
}
}
}
// 验证密码输入合法行和与确认密码是否一致
function checkpwd(pwd) {
var qrpwd = $.trim($( ' #<%=txtqrpwd.ClientID %> ' ).val());
if (pwd.length == 0 ) {
$( " #mecpwd " ).html( " <span style='color:red;'>密码不能为空</span> " );
return false ;
}
var reg = / ^[a-zA-Z0-9]{6,20}$ / g;
if ( ! reg.test(pwd)) {
$( " #mecpwd " ).html( " <span style='color:red;'>密码长度必须大于6个字符小于20个字符,只能为英语字母或者数字</span> " );
return false ;
}
else {
$( " #mecpwd " ).html( " <img src=images/gou.gif></a> " );
}
}
// 验证提示问题
function checkquestion(question) {
if (question.length != 0 ) {
$( " #mecquestion " ).html( " <img src=images/gou.gif></a> " );
}
else {
$( " #mecquestion " ).html( " <span style='color:red;'>确认密码不一致</span> " );
}
}
// 验证问题答案
function checkanswer(answer) {
if (answer.length != 0 ) {
$( " #mecanswer " ).html( " <img src=images/gou.gif></a> " );
}
else {
$( " #mecanswer " ).html( " <span style='color:red;'>确认密码不一致</span> " );
}
}
</ script >
</ asp:Content >
< asp:Content ID ="Content2" ContentPlaceHolderID ="ContentPlaceHolder1" runat ="server" >
< table width ="789" border ="1" >
< tr >
< td align ="left" valign ="top" >
< table width ="100%" border ="0" >
< tr >
< td height ="35" bgcolor ="#CCCCCC" style ="font-weight: bold; padding-left: 10px;" >
< img src ="images/o.gif" width ="9" height ="9" />
新用户注册
</ td >
</ tr >
< tr >
< td align ="left" >
< table align ="center" border ="0" cellpadding ="1" cellspacing ="1" width ="70%" >
< tr >
< td >
< span class ="pad" > 用 户 名:
< asp:TextBox ID ="txtname" runat ="server" onblur ="checkname(this.value);" ></ asp:TextBox >
< span id ="meusername" > * </ span >< br />
请填写您用来登录的用户名。 </ span >
</ td >
</ tr >
< tr >
< td >
< span class ="pad" > 电子邮件:
< asp:TextBox ID ="txtemail" runat ="server" onblur ="checkemail(this.value);" ></ asp:TextBox >
< span id ="mecemail" > * </ span >< br />
< br />
</ span >
</ td >
</ tr >
< tr >
< td >
< span class ="pad" > 密 码:
< asp:TextBox ID ="txtpwd" TextMode ="Password" runat ="server" onblur ="checkpwd(this.value);" ></ asp:TextBox >
< span id ="mecpwd" > * </ span >< br />
< br />
长度必须大于6个字符小于20个字符,只能为英语字、数字,例如:snsn2003等。 </ span >
</ td >
</ tr >
< tr >
< td height ="35" >
< span class ="pad" > 确认密码:
< asp:TextBox ID ="txtqrpwd" TextMode ="Password" runat ="server" onblur ="checkqrpwd(this.value);" ></ asp:TextBox >
< span id ="mecqrpwd" > * </ span >< br />
< br />
请将输入的密码再次输入,以免设置密码出错。 </ span >
</ td >
</ tr >
< tr >
< td >
< span class ="pad" > 密码提问:
< asp:TextBox ID ="txtquestion" runat ="server" onblur ="checkquestion(this.value);" ></ asp:TextBox >
< span id ="mecquestion" > * </ span >< br />
(用于密码遗忘时使用) </ span >
</ td >
</ tr >
< tr >
< td >
< span class ="pad" > 密码答案:
< asp:TextBox ID ="txtanswer" runat ="server" onblur ="checkanswer(this.value);" ></ asp:TextBox >
< span id ="mecanswer" > * </ span >< br />
(密码遗忘时将验证此答案) </ span >
</ td >
</ tr >
< tr >
< td >
< span class ="pad" >
< asp:Button ID ="butsubmit" runat ="server" OnClientClick ="return checkform()" Text ="提交"
OnClick ="butcz" /> < asp:Button ID ="Button5" runat ="server" Text ="重置" OnClick ="Button5_Click" />
< asp:Button ID ="Button6" runat ="server" Text ="返回" OnClick ="Button6_Click" />
(带*为必填项) </ span >
</ td >
</ tr >
</ table >
</ td >
</ tr >
</ table >
</ td >
</ tr >
</ table >
</ asp:Content >
在headler文件夹下建立的checkusername.ashx页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace xh.shop.web.handler
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
public class checkusername : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = " text/plain " ;
string name = context.Request.QueryString[ " username " ];
if ( string .IsNullOrEmpty(name))
{
context.Response.Write( " false " );
context.Response.End();
}
if ( new xh.shop.DAL.user().Exists(name))
{
context.Response.Write( " false " );
context.Response.End();
}
else
{
context.Response.Write( " true " );
context.Response.End();
}
}
public bool IsReusable
{
get
{
return false ;
}
}
}
}