CustomValidator ScriptManager+webservice验证用户名是否存在

晨曦之光 发布于 2012/05/16 17:15
阅读 147
收藏 0

实现功能:

             1.用户输入用户名检查字数如果小于6或者大于12提示“用户名必须在6到12个字之间”

             2.1成立,判断用户名是否存在,存在提示“已经被占用”

CustomValidatorDemo.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomValidatorDemo.aspx.cs" Inherits="CustomValidatorDemo" %>

<!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></title>
    <script language="javascript" type="text/javascript">
        function ClientValidatorName(source, args) {
            var name = document.getElementById("tbName");
            if (name.value.length < 6) {
                args.IsValid = false;
            }
            else if (name.value.length > 12) {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }
        }
        function JSCheckUser() {
            var name = document.getElementById("tbName");
            var lab = document.getElementById("Label1");
            if (name.value.length > 6 && name.value.length < 12) {
                lab.style.display = "";
                var name = $get("tbName");
                WSValidator.CheckUser(name.value, OnSuccess)
            }
            else {
                lab.style.display = "none";
            }
        }
        function OnSuccess(result) {
            var lab = $get("Label1");
            lab.innerHTML = result;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="~/WSValidator.asmx" />
        </Services>
    </asp:ScriptManager>
    <div>
        <asp:TextBox ID="tbName" runat="server" onblur="JSCheckUser()"></asp:TextBox>
        <asp:CustomValidator ID="CustomValidator1" runat="server"  ClientValidationFunction="ClientValidatorName" Display="Dynamic"
            ErrorMessage="用户名必须在6到12个字之间" ControlToValidate="tbName"></asp:CustomValidator>
        <asp:Label ID="Label1" runat="server" Text="Label" style="display:none"></asp:Label>
        <asp:Button ID="btnOk" runat="server" Text="提交" />
    </div>
    </form>
</body>
</html>

 

WSValidator.asmx

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
///WSValidator 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WSValidator : System.Web.Services.WebService
{

    public WSValidator()
    {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod]
    public string CheckUser(string name)
    {
        if (name == "admin123")
        {
            return name + "可以使用";
        }
        else
        {
            return name + "已经被占用";
        }
    }
}

 


原文链接:http://blog.csdn.net/maji9370/article/details/5627795
加载中
返回顶部
顶部