简体   繁体   中英

Replace custom validator in server side with client side

I have a server side 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? 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. You would call this web method trough an ajax call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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