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

实现功能:

             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" οnblur="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 + "已经被占用";
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值