加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 02:02:12 所属栏目:安全 来源:网络整理
导读:实现功能: ???????????? 1.用户输入用户名检查字数如果小于6或者大于12提示“用户名必须在6到12个字之间” ?????????????2.1成立,判断用户名是否存在,存在提示“已经被占用” CustomValidatorDemo.aspx %@ Page Language="C#" AutoEventWireup="true" Cod

实现功能:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读