自定义验证器客户端函数

发布于 2024-09-18 12:57:51 字数 2599 浏览 7 评论 0原文

我有一个自定义验证器(.net 3.5),它检查表单中的四个下拉列表是否具有重复值。它可以在服务器端运行,但我想添加一个客户端功能来配合它。我对 JavaScript 毫无了解。你能帮忙吗?曼特谢谢。

 <asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage = "Same related document was entered more than once" OnServerValidate="dropDownValidation_ServerValidate" Display="Dynamic"></asp:CustomValidator>


   Protected Sub dropDownValidation_ServerValidate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
    e.IsValid = Not haveSameValue(DropDownList9.SelectedValue, DropDownList12.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList9.SelectedValue, DropDownList15.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList9.SelectedValue, DropDownList18.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList12.SelectedValue, DropDownList15.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList12.SelectedValue, DropDownList18.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList15.SelectedValue, DropDownList18.SelectedValue) 
    End Sub

    Protected Function haveSameValue(ByVal first As String, ByVal second As String) As Boolean
    If first <> "" And second <> "" AndAlso first.Equals(second) Then
        Return first.Equals(second)
    End If
   End Function

更新:以下 JavaScript 代码可以正常工作,因为它检查下拉列表中是否存在重复值。但是,我如何将其链接到我的自定义验证器并消除警报消息。按照现在的情况,页面已提交。谢谢。

  function dropDownValidation_ClientValidate() { 

    var strValue1 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList1');  
    var strValue2 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList2'); 
    var strValue3 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList3'); 
    var strValue4 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList4'); 

         var result = haveSameValue(strValue1.value, strValue2.value) &&
          haveSameValue(strValue1.value, strValue3.value) &&
          haveSameValue(strValue1.value, strValue4.value) &&
          haveSameValue(strValue2.value, strValue3.value) &&
          haveSameValue(strValue2.value, strValue4.value) &&
          haveSameValue(strValue3.value, strValue4.value);

          return result; 


           }

function haveSameValue(ddlValue1, ddlValue2) {

  if (ddlValue1 != null && ddlValue1 != '' && ddlValue2 != null && ddlValue2 != '' && ddlValue1 == ddlValue2){
    alert("Related documents contain duplicate values");

    }

}

I have a custom validator (.net 3.5) that checks if four dropdown lists in my form have repeated values. It works on the server-side but I would like to add a client-side function to go with it. I have no knowledge of JavaScript. Could you help? Mant thanks.

 <asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage = "Same related document was entered more than once" OnServerValidate="dropDownValidation_ServerValidate" Display="Dynamic"></asp:CustomValidator>


   Protected Sub dropDownValidation_ServerValidate(ByVal sender As Object, ByVal e As ServerValidateEventArgs)
    e.IsValid = Not haveSameValue(DropDownList9.SelectedValue, DropDownList12.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList9.SelectedValue, DropDownList15.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList9.SelectedValue, DropDownList18.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList12.SelectedValue, DropDownList15.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList12.SelectedValue, DropDownList18.SelectedValue) AndAlso _
                Not haveSameValue(DropDownList15.SelectedValue, DropDownList18.SelectedValue) 
    End Sub

    Protected Function haveSameValue(ByVal first As String, ByVal second As String) As Boolean
    If first <> "" And second <> "" AndAlso first.Equals(second) Then
        Return first.Equals(second)
    End If
   End Function

UPDATE: The following JavaScript code works ok as it checks if there are duplicate values in the dropdown lists. However how can I link this to my custom validator and eliminate the alert message. As it stands now the page gets submitted. Thanks.

  function dropDownValidation_ClientValidate() { 

    var strValue1 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList1');  
    var strValue2 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList2'); 
    var strValue3 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList3'); 
    var strValue4 = document.getElementById('ctl00_ContentPlaceHolder1_DropDownList4'); 

         var result = haveSameValue(strValue1.value, strValue2.value) &&
          haveSameValue(strValue1.value, strValue3.value) &&
          haveSameValue(strValue1.value, strValue4.value) &&
          haveSameValue(strValue2.value, strValue3.value) &&
          haveSameValue(strValue2.value, strValue4.value) &&
          haveSameValue(strValue3.value, strValue4.value);

          return result; 


           }

function haveSameValue(ddlValue1, ddlValue2) {

  if (ddlValue1 != null && ddlValue1 != '' && ddlValue2 != null && ddlValue2 != '' && ddlValue1 == ddlValue2){
    alert("Related documents contain duplicate values");

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

長街聽風 2024-09-25 12:57:51

在不使用任何库(例如 jQuery)的情况下,最简单的方法可能是这样的:

HTML:

<select id="dropdown1">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>
<select id="dropdown2">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>
<select id="dropdown3">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>
<select id="dropdown4">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>
<button id="submitbutton">Submit</button>

JavaScript validator:

var validator = function() {
    var objChosenItems = {};
    var strDuplicateItem = null;
    for (var i = 1; i <= 4; i++) {
        var strValue = document.getElementById('dropdown' + i.toString()).value;
        if (objChosenItems[strValue]) {
            strDuplicateItem = strValue;
            break;
        }
        objChosenItems[strValue] = true;
    }
    if (strDuplicateItem === null) {
        return true;
    }
    else {
        alert("DUPLICATE ITEM: " + strDuplicateItem);
        return false;
    }
};

您需要将 validator 连接到按钮,它可以是

document.getElementById('submitbutton').onclick = validator;

<input type="button" onclick="validator()" />

<form action="mysite.aspx" method="post" onsubmit="validator()">

Without using any libraries (such as jQuery), the simplest way is probably this:

HTML:

<select id="dropdown1">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>
<select id="dropdown2">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>
<select id="dropdown3">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>
<select id="dropdown4">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
</select>
<button id="submitbutton">Submit</button>

JavaScript validator:

var validator = function() {
    var objChosenItems = {};
    var strDuplicateItem = null;
    for (var i = 1; i <= 4; i++) {
        var strValue = document.getElementById('dropdown' + i.toString()).value;
        if (objChosenItems[strValue]) {
            strDuplicateItem = strValue;
            break;
        }
        objChosenItems[strValue] = true;
    }
    if (strDuplicateItem === null) {
        return true;
    }
    else {
        alert("DUPLICATE ITEM: " + strDuplicateItem);
        return false;
    }
};

You need to wire up validator to the button, which could be a <input type="submit/image/etc" /> or a <button> or several other things. Whatever it is, you can just bind the onclick event to the element. E.g.:

document.getElementById('submitbutton').onclick = validator;

Or

<input type="button" onclick="validator()" />

Or

<form action="mysite.aspx" method="post" onsubmit="validator()">
昵称有卵用 2024-09-25 12:57:51

我认为你需要这样的东西:

<asp:CustomValidator id="CustomValidator1" runat="server" 
    ErrorMessage = "Same related document was entered more than once" 
    OnServerValidate="dropDownValidation_ServerValidate" Display="Dynamic"
    ClientValidationFunction="dropDownValidation_ClientValidate();">
</asp:CustomValidator>

<script type="text/javascript">
    function dropDownValidation_ClientValidate() {
        var result =
            (haveSameValue('<%= DropDownList1.ClientID %>', '<%= DropDownList2.ClientID %>') == false) &&
            (haveSameValue('<%= DropDownList2.ClientID %>', '<%= DropDownList3.ClientID %>') == false) &&
            (haveSameValue('<%= DropDownList3.ClientID %>', '<%= DropDownList4.ClientID %>') == false) &&
            (haveSameValue('<%= DropDownList4.ClientID %>', '<%= DropDownList5.ClientID %>') == false);
        return result;
    }

    function haveSameValue(ddl1, ddl2) {
        var result = false;
        var value1 = document.getElementById(ddl1).value;
        var value2 = document.getElementById(ddl2).value;

        if (value1 != null && value1 != '' && value2 != null && value2 != '' && value1 == value2) {
            result = true;
        }
        return result;
    }
</script>

你能检查一下它是否有效吗?

You need this kind of thing I think:

<asp:CustomValidator id="CustomValidator1" runat="server" 
    ErrorMessage = "Same related document was entered more than once" 
    OnServerValidate="dropDownValidation_ServerValidate" Display="Dynamic"
    ClientValidationFunction="dropDownValidation_ClientValidate();">
</asp:CustomValidator>

<script type="text/javascript">
    function dropDownValidation_ClientValidate() {
        var result =
            (haveSameValue('<%= DropDownList1.ClientID %>', '<%= DropDownList2.ClientID %>') == false) &&
            (haveSameValue('<%= DropDownList2.ClientID %>', '<%= DropDownList3.ClientID %>') == false) &&
            (haveSameValue('<%= DropDownList3.ClientID %>', '<%= DropDownList4.ClientID %>') == false) &&
            (haveSameValue('<%= DropDownList4.ClientID %>', '<%= DropDownList5.ClientID %>') == false);
        return result;
    }

    function haveSameValue(ddl1, ddl2) {
        var result = false;
        var value1 = document.getElementById(ddl1).value;
        var value2 = document.getElementById(ddl2).value;

        if (value1 != null && value1 != '' && value2 != null && value2 != '' && value1 == value2) {
            result = true;
        }
        return result;
    }
</script>

Can you check if it works?

场罚期间 2024-09-25 12:57:51

这是旧的,但我想在这里给出一个答案,因为我是在谷歌搜索类似问题时得到的。

在您的 Javascript 函数中,它将采用 2 个参数:source 和 args。您需要根据验证是否成功将 args.IsValid 设置为 true 或 false。如果您正在验证特定字段,则 args.Value 将是正在验证的表单字段的值。

一个简单的验证器函数来检查表单是否等于“1”,如下所示:

function validateThis(source, args) { 
    if (args.Value == "1") 
        args.IsValid = true; 
    else 
        args.IsValid = false;
}

将其与上面的答案结合起来并设置 args.IsValid 而不是返回值,您应该得到答案。

更多信息可以在此中找到4guysfromrolla.com 文章

This is old, but I wanted to put an answer here, as I got it in response to a Google search for a similar question.

In your Javascript function, it will take 2 parameters, source and args. You want to set args.IsValid to either true or false depending on whether or not validation succeeded or not. args.Value will be the value of the form field being validated if you are validating a specific field.

A simple validator function to check if the form was equal to "1" would look like this:

function validateThis(source, args) { 
    if (args.Value == "1") 
        args.IsValid = true; 
    else 
        args.IsValid = false;
}

Combine this with the above answers and set args.IsValid rather than the return, and you should have your answer.

More information can be found in this 4guysfromrolla.com article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文