将服务器端的自定义验证器替换为客户端

Replace custom validator in server side with client side

本文关键字:替换 客户端 验证 服务器端 自定义      更新时间:2023-09-26

我有一个服务器端定制器。因为它没有触发,所以我想使用客户端来验证文本框。它包括活动目录的东西。我不确定我们可以将代码转换为客户端吗?

<td class="style4">
            <asp:TextBox ID="TextUserName" runat="server"></asp:TextBox>
        </td><td><asp:CustomValidator ID="CustomValidatorUser" runat="server" ControlToValidate="TextUserName"
                                ErrorMessage="Minimum of 6 (six) alphanumeric characters." 
                OnServerValidate="ValidateUser" Display="Dynamic"
                                ValidateEmptyText="True" ></asp:CustomValidator></td>

 protected void ValidateUser(object source, ServerValidateEventArgs args)
 {
        string UserNameCreated = TextUserName.Text;
        string AD_Server = System.Configuration.ConfigurationManager.AppSettings["AD_Server"];
        DirectoryEntry entry = new DirectoryEntry(AD_Server);
        entry.AuthenticationType = AuthenticationTypes.Secure;
        DirectorySearcher deSearch = new DirectorySearcher(entry);
        deSearch.Filter = "(&(objectClass=user)(samaccountname=" + UserNameCreated + "))";
        SearchResultCollection results = deSearch.FindAll();
        Match match = Regex.Match(args.Value, @"^[a-zA-Z0-9]{6,}$",
    RegexOptions.IgnoreCase);
            if (results.Count > 0)
            args.IsValid = false;
        else if (match.Success)
            args.IsValid = true;
        // true means that it is validated. 
        else
            args.IsValid = false;
 }

我的想法:

拳头:

<td class="style4">
            <asp:TextBox ID="TextUserName" runat="server"></asp:TextBox>
        </td><td><asp:CustomValidator ID="CustomValidatorUser" runat="server" ControlToValidate="TextUserName"
                                ErrorMessage="Minimum of 6 (six) alphanumeric characters." 
                ClientValidatationFunction="ValidateUser" Display="Dynamic"
                                ValidateEmptyText="True" ></asp:CustomValidator></td>

第二

<script language="javascript"> 
function ValidateUser(source, arguments)
{
    var RegularExpression = /^[a-zA-Z0-9]{6,}$/;
    if (arguments.Value.test(RegularExpression) == 0 ){
        arguments.IsValid = true;
    } else {
        arguments.IsValid = false;
    }
}
</script>

那么AD呢?也许这是一个错误的问题!!

谢谢。

我不知道

您打算如何使用此代码,如果它与任何方面的身份验证有关,您应该在服务器端执行所有验证,因为客户端代码很容易被破解。

话虽如此,如果安全性不是问题,您可以在服务器上使用Web方法来获取结果计数。您可以通过 ajax 调用调用此 Web 方法。