基于ajax的数据验证
生活随笔
收集整理的這篇文章主要介紹了
基于ajax的数据验证
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
實(shí)現(xiàn)代碼:
aspx代碼如下:
<%@?Page?Language="C#"?AutoEventWireup="true"?CodeFile="Verify.aspx.cs"?Inherits="Verify"?%>
<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html?xmlns="http://www.w3.org/1999/xhtml"?>
<head?runat="server">
?????<title>Verify</title>
????<script?language="jscript">
????function?VerifyUsername(name)
????{
????????Verify.GetReturnCode(name,IsUsernameExist_callback);
????}
????
????function?IsUsernameExist_callback(res)
????{
????????var?msg?=?document.getElementById("lblMessage");
????????var?bRet?=?res.value;
????????if(bRet?==?"0")
????????{
????????????msg.innerHTML?=?"用戶名存在!";
????????????msg.style.color?=?"green";
????????}
????????else?if(bRet?==?"1")
????????{
????????????msg.innerHTML?=?"用戶名長(zhǎng)度必須在3到15之間,且不包含字母、數(shù)字和下劃線以外的字符!";
????????????msg.style.color?=?"red";
????????}
????????else
????????{
????????????msg.innerHTML?=?"用戶名不存在!";
????????????msg.style.color?=?"red";
????????}
????}
????</script>
</head>
<body>
????<form?id="form1"?runat="server">
????<div>
????????<h1>基于Ajax的數(shù)據(jù)驗(yàn)證</h1>
????????<hr?/>
????????<asp:Label?ID="Label1"?runat="server">用戶名:</asp:Label>
????????<input?type="text"?id="tbUsername"?onkeyup="VerifyUsername(this.value)"?/><br?/>
????????<asp:Label?ID="lblMessage"?runat="server"></asp:Label>
????</div>
????</form>
</body>
</html>
后臺(tái)C#代碼:
using?System;
using?System.Data;
using?System.Configuration;
using?System.Collections;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
using?System.Text.RegularExpressions;
public?partial?class?Verify?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????AjaxPro.Utility.RegisterTypeForAjax(typeof(Verify));
????????//作用是向客戶端輸出下面的代碼:
????????//<script?type="text/javascript"?src="/ajaxprotest/ajaxpro/prototype.ashx"></script>
????????//<script?type="text/javascript"?src="/ajaxprotest/ajaxpro/core.ashx"></script>
????????//<script?type="text/javascript"?src="/ajaxprotest/ajaxpro/converter.ashx"></script>
????????//<script?type="text/javascript"?src="/ajaxprotest/ajaxpro/Verify,App_Web_elgsobcn.ashx"></script>
????}
????//加上[AjaxPro.AjaxMethod],才能被客戶端訪問(wèn)
????[AjaxPro.AjaxMethod]
????public?string?GetReturnCode(string?strUsername)
????{
????????if?(!IsValidUsername(strUsername))
????????{
????????????return?"1";
????????}
????????else?if?(!IsUsernameExist(strUsername))
????????{
????????????return?"2";
????????}
????????else
????????{
????????????return?"0";
????????}
????}
????private?bool?IsUsernameExist(string?strUsername)
????{
????????bool?bRet?=?false;
????????switch?(strUsername.ToLower())
????????{
????????????case?"test":
????????????case?"tom":
????????????case?"jack":
????????????case?"ajax":
????????????case?"aspnet":
????????????case?"nic":
????????????????bRet?=?true;
????????????????break;
????????}
????????return?bRet;
????}
????private?bool?IsValidUsername(string?strUsername)
????{
????????return?(Regex.IsMatch(strUsername,?@"^(\w{3,15})$"));
????}
????private?bool?IsValidEmail(string?strEmail)
????{
????????return?(Regex.IsMatch(strEmail,?@"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$"));
????}
}
說(shuō)明:我這里使用了ajaxpro.net庫(kù)進(jìn)行開(kāi)發(fā),大家可以在網(wǎng)絡(luò)上找找最新版的ajaxpro.net庫(kù),下載后在項(xiàng)目里引用就可以了!
轉(zhuǎn)載于:https://www.cnblogs.com/huanggenying/archive/2007/11/28/975154.html
總結(jié)
以上是生活随笔為你收集整理的基于ajax的数据验证的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 正确使用 SharePoint 对象避免
- 下一篇: 计算机组成原理双端口存储器实验,计算机组