此程序包括三部分: aspx页面, js(zp_Validator.js)和一般处理程序ashx(Search.ashx)
按钮btnRepeat 用于检查用户名是否存在
按钮 btnRegister 检查信息是否填写完整;
1. ) aspx界面设置如图所示;并添加js链接 并在page_load中添加触发事件
<script type="text/javascript" src="script/zp_Validator.js"></script>
if (!Page.IsPostBack)
{
this.btnRepeat.Attributes.Add("onclick", "return repeatID();");
this.btnRegister.Attributes.Add("onclick", "return ValidatorRegister();");
}
2.) zp_Validator.js代码如下:
var xmlHttp;
function createXMLHttpRequest() {
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
alert("对不起,您的浏览器不支持XMLHttpRequest对象!");
}
}
}
}
//用于验证用户名是否重复
function repeatID() {
createXMLHttpRequest();
var url = "../Search.ashx?Name=" + document.getElementById("txtName").value;
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange = repeatResult;
xmlHttp.send("");
return false;
}
function repeatResult() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
if (xmlHttp.responseText == "Duan") {
alert("对不起,您输入的用户名小于3个字符, 请输入一个较长的用户名。");
document.getElementById("txtName").focus();
}
else {
if (xmlHttp.responseText == "You") {
alert("对不起,您输入的用户名,已经被他人使用。");
document.getElementById("txtName").focus();
}
else {
if (xmlHttp.responseText == "Wu") {
alert("恭喜,帐号不已存在,可以放心注册");
document.getElementById("txtName").focus();
}
}
}
}
}
}
//验证完整性
function ValidatorRegister() {
if (RemoverSpace(document.getElementById("txtName").value).length < 3) {
alert("对不起,您输入的用户名小于3个字符, 请输入一个较长的用户名!");
document.getElementById("txtName").focus();
return false;
}
if (!isDigit(document.getElementById("txtPwd").value)) {
alert("密码只能是6-20位的英文字母或数字,以下划线或字母开头!或者2-10位汉字");
document.getElementById("txtPwd").focus();
return false;
}
if (document.getElementById("txtPwd2").value != document.getElementById("txtPwd").value) {
alert("确认密码错误!");
document.getElementById("txtPwd2").focus();
return false;
}
if (!IsMail(document.getElementById("txtEmail").value)) {
alert("E-mail格式不正确!");
document.getElementById("txtEmail").focus();
return false;
}
}
function LoginValidator() {
if (RemoverSpace(document.getElementById("txtName").value).length == 0) {
alert("请输入用户名!");
document.getElementById("txtName").focus();
return false;
}
if (document.getElementById("txtPwd").value.length==0 ) {
alert("请输入密码!");
document.getElementById("txtPwd").focus();
return false;
}
}
//Email验证
function IsMail(mail) {
var patrn = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
if (!patrn.test(mail))
return false;
else
return true;
}
//验证空格
function RemoverSpace(txt) {
return txt.replace(/^\s+|\s+$/g, '');
}
function isDigit(s) {
var patrn = /^[a-zA-Z_]\w{5,20}|[\u0391-\uFFE5]{1,10}$/;
if (!patrn.exec(s)) return false
return true
}
3.) 一般处理程序Search.ashx代码如下:
<%@ WebHandler Language="C#" Class="Search" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using zp_Model;
using zp_Biz;
public class Search : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//前台输入
string Name = context.Request.QueryString["Name"].ToString();
if (Name.Trim().Length < 3)
{
context.Response.Write("Duan");
}
else
{
zp_User be = new zp_User();
be.User_Name = Name;
zp_UserBiz biz = new zp_UserBiz();
bool flag = biz.User_Repeat(be);//查询是据库是否已经存在
if (flag)
{
context.Response.Write("You");
}
else
{
context.Response.Write("Wu");
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}