简体   繁体   English

通过JavaScript禁用转发器内的特定验证器

[英]Disable specific Validators inside a repeater via Javascript

I have some fields in a repeater and I need to validate them. 我在中继器中有一些字段,我需要对其进行验证。

Here is the scenario: When the page loads I get a set of fields just once(first name, last name etc.) and I get a link "Add another user", if you click the link it adds the same fields again on the bottom. 场景如下:当页面加载时,我仅获得了一组字段(名字,姓氏等),并且获得了“添加其他用户”链接,如果单击该链接,它将再次在页面上添加相同的字段。底部。

Now for the code part: In my case I needed to run the repeater 4 times (so the fields are on the page 4 times from the begining). 现在是代码部分:在我的情况下,我需要运行转发器4次(因此,页面上的字段从一开始就运行4次)。 Then I hide them as I hide the <div> that contains them. 然后隐藏它们,就像隐藏包含它们的<div>一样。 When the button is clicked I show the first hidden div and so on. 单击按钮后,我将显示第一个隐藏的div,依此类推。

Some code(not all): 一些代码(不是全部):

 <asp:Repeater ID="rptOtherPeople" runat="server">
 <HeaderTemplate>
      <table>
           <thead>
                <tr>
                     <td>
                          <h3>Други лица</h3>
                     </td>
                </tr>
           </thead>
           <tbody class="GridBody"> 
 </HeaderTemplate>
 <ItemTemplate>
      <tr class="GridRow" id="personRow" style="display: none">
           <td>
                <asp:TextBox ID="txtFirstName" CssClass="mid-inp" Text="" runat="server"></asp:TextBox>
           </td>
      </tr>
 </ItemTemplate>
 <FooterTemplate>
    </tbody>
    </table>
 </FooterTemplate>
 </asp:Repeater>

And here is that javascript that shows the next row: 这是显示下一行的javascript:

$(document).ready(function () {
        var peopleNum = 1;

        if ($(".GridBody tr").length > 0) {
            var tr = $(".GridBody tr")[0];
            tr.style.display = 'table-row';
            tr.setAttribute('hidden', 'false');
            var anc = tr.getElementsByTagName('a');
        }

        if ($(".GridBody tr").length > 0 && peopleNum > 0) {
            for (i = 0; i < peopleNum; i++) {
                var tr = $(".GridBody tr")[i];
                tr.style.display = 'table-row';
                tr.setAttribute('hidden', 'false');
                if (i > 0) {
                    var anc = tr.getElementsByTagName('a');
                    if (anc[i] != undefined)
                        anc[i].style.display = 'none';
                }
            }
        }
    })

    function addPerson() {
        var body = $(".GridBody");
        var indexOfNextRow = $('tr[class="GridRow"][hidden="false"]').length;
        var tr = $(".GridBody tr")[indexOfNextRow];
        tr.style.display = 'table-row';
        tr.setAttribute('hidden', 'false');
    }

The Problem: For example I want the field to be required. 问题:例如,我希望该字段为必填项。 I put a RequiredFieldValidator and I disable it in some cases and enable it in others. 我放置了RequiredFieldValidator并在某些情况下将其禁用,而在其他情况下将其启用。 The thing is that I get 4 RequiredFieldValidators on the page and I can only turn ALL of them on or off at once. 事实是,我在页面上获得了4个RequiredFieldValidators,并且一次只能打开或关闭所有它们。 I want to activate just one of them. 我只想激活其中之一。 I couldn't find a way to do that because they are pretty much identical. 我找不到办法,因为它们几乎完全相同。 Any ideas? 有任何想法吗?

I assume that I can not sort this out in the code behind. 我假设我无法在后面的代码中对此进行分类。 Can I work with just one RequiredFieldValidator via javascript(how do I identify the one I want). 我可以通过javascript处理一个RequiredFieldValidator吗(如何识别我想要的一个)。 Some people prefer jquery validation. 有些人喜欢jquery验证。 Is it applicable in this case and how(I have never used jquery validation before)? 它在这种情况下适用吗?如何使用(我以前从未使用过jquery验证)?

EDIT 1 编辑1

Ok the controls are not identical. 好的,控件并不相同。 In the browser the generated ID is: ctl00_SPWebPartManager1_g_f6926ea5_98ba_46c1_b157_4f1ddc46885d_ctl00_Step21_otherPeople_rptOtherPeople_ctl01_rv1 , but I can not access the validator from here in my Javascript 在浏览器中,生成的ID为: ctl00_SPWebPartManager1_g_f6926ea5_98ba_46c1_b157_4f1ddc46885d_ctl00_Step21_otherPeople_rptOtherPeople_ctl01_rv1 ,但是我无法从此处通过Javascript访问验证器

You can disable the validators either server side or client side.If i understood your question , the thing you looking for is disabling a specific vaidator say required field validator.For that here is a simple javascript code to disable the validators. 您可以在服务器端或客户端禁用验证器。如果我理解您的问题,那么您要查找的就是禁用特定验证器,说必填字段验证器。为此,这是一个简单的JavaScript代码来禁用验证器。

    function DisableRFValidators() {
        var ValidatorToDisable = document.getElementById("RequiredFieldValidator2");
        ValidatorEnable(ValidatorToDisable, false);
    }

Fixed it! 修复! Here is the code: 这是代码:

    $("#aspnetForm").validate();

    $(".required").each(function (index) {
        if ($(this).attr("id").indexOf("txtFirstName") >= 0) {
            $(this).rules("add", {
                required: true,
                minlength: 3,
                messages: {
                    required: "<div class='val' style='color:red'>Name is Required!</div>",
                    minlength: "<div class='val' style='color:red'>Minimum number of symbols = 3!</div>"
                }
            });
        }
        else if ($(this).attr("id").indexOf("txtFirstName") >= 0){
            $(this).rules("add", {
                required: false
            });
        }
    });

function validateData() {
    var result = $("#aspnetForm").valid();
    return result;
}

function btnNextClick(btn_this) {
    var btnNext = document.getElementById("<%=btnMoveNextHidden.ClientID%>");
    if (btnNext != null && validateData() == true) {
        btnNext.click();
    }
}

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

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