簡體   English   中英

用客戶端替換服務器端的自定義驗證器

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

我有一個服務器端的customvalidator。 因為它不觸發,所以我想使用客戶端來驗證文本框。 它包括活動目錄。 我不確定是否可以將代碼轉換為客戶端?

<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方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM