微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐