简体   繁体   English

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

[英]Replace custom validator in server side with client side

I have a server side customvalidator. 我有一个服务器端的customvalidator。 Because it is not firing, so I want to use client side to validate a textbox. 因为它不触发,所以我想使用客户端来验证文本框。 It include active directory something. 它包括活动目录。 I am not sure can we convert the code to client side? 我不确定是否可以将代码转换为客户端?

<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>

And

 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;
 }

My thought: 我的想法:

Fist: 拳头:

<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>

Second 第二

<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>

Then how about AD? 那AD呢? Maybe it is a wrong question!! 也许这是一个错误的问题!

Thanks. 谢谢。

I don't know exaclty how are you planning to use this code, if it's related to authentication in any aspect you should perform all validations on server side since client code can be easily cracked. 我不知道您打算如何使用此代码,如果它与身份验证有任何关系,则应在服务器端执行所有验证,因为可以轻松破解客户端代码。

With this said, in case the security is not a problem, you could have a web method on the server to get results count. 这样说来,如果安全性不是问题,则可以在服务器上使用Web方法来获取结果计数。 You would call this web method trough an ajax call. 您将通过ajax调用来调用此Web方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM