後台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;
}
}
}
}