简体   繁体   English

在按钮提交时触发必填字段验证器

[英]Required field validator to be fired on button submit

I have a dropdown list, where when I select OPTION value a Textbox appears. 我有一个下拉列表,当我选择OPTION值时,将出现一个文本框。 The issue is that when, I submit directly the required filed errors gives me message for both ie for dropdown and Textbox. 问题是,当我直接提交所需的错误时,会同时显示有关下拉列表和文本框的消息。 It should only give error for dropdown. 它应该只给出错误的下拉菜单。 As soon as I select OPTION from dropdown it directly gives me error message for textbox. 从下拉菜单中选择“选项”后,它将直接为我显示文本框错误消息。 It should give me after when I dont put any value in the textbox and directly submit the form. 当我没有在文本框中输入任何值并直接提交表单时,它应该给我。 I tried with the code but it wasn't working as required. 我尝试使用该代码,但未按要求工作。 Please see the code below: 请参见下面的代码:

HTML for dropdown & Textbox 下拉菜单和文本框的HTML

<asp:DropDownList ID="ddlGraduation" runat="server" CssClass="txtfld-popup_drp1"></asp:DropDownList>

<asp:RequiredFieldValidator CssClass="error_msg" ID="reqGraduation" runat="server" ControlToValidate="ddlGraduation" ErrorMessage="Please select graduation details" InitialValue="--Select--" SetFocusOnError="true"></asp:RequiredFieldValidator>


<asp:TextBox ID="txtOther" runat="server" CssClass="txtfld-popup_p1" Style="display: none;"></asp:TextBox>

<asp:RequiredFieldValidator ID="reqOther" runat="server" ControlToValidate="txtOther" ErrorMessage="Please specify your qualification" Enabled="false"></asp:RequiredFieldValidator>

Also, see the javascript code for handling the textbox visiblity & firing of required field:- 另外,请参见用于处理文本框可见性和触发必填字段的javascript代码:-

<script type="text/javascript">
    $(document).ready(function () {
    $("select[id*=ctl00_ContentPlaceHolder1_ddlGraduation]").change(function () {
    if ($('#ctl00_ContentPlaceHolder1_ddlGraduation').val() == 'Other') {
        $("[id*=ctl00_ContentPlaceHolder1_txtOther]").show();
        var valEmail = $("[id*=reqOther]");
        ValidatorEnable(valEmail[0],true);
    }
    else {
        var valEmail = $("[id*=reqOther]");
        ValidatorEnable(valEmail[0],
        false);
        $("[id*=ctl00_ContentPlaceHolder1_txtOther]").hide();
    }
});

}); });

Please help 请帮忙

Once your validation fires then you can not submit the form without entering valid data. 验证一旦触发,您就无法在不输入有效数据的情况下提交表单。 And one more thing, RequiredFieldValidator will fire every time whether you want to display the text box or not. 还有一件事,无论您是否要显示文本框,RequiredFieldValidator都会在每次触发。 It is not like that if you will hide your text box and validation will not fire, it should not be the case. 不是那样的话,如果您将隐藏自己的文本框并且不会触发验证,则情况并非如此。 The solution to your problem is keep the RequiredFieldValidator only for drop down and remove it for text box and add one custom validator in which you can check the field is empty or not based on the conditions and the custom validator will fire on click of "Submit" button. 您问题的解决方案是仅保留RequiredFieldValidator下拉框,然后将其删除以添加文本框,然后添加一个自定义验证器,您可以根据条件在其中检查字段是否为空,并且在单击“提交”后将触发自定义验证器”按钮。

<asp:CustomValidator ID="reqOther" runat="server" EnableClientScript="true"
    ErrorMessage="Please specify your qualification"
    ClientValidationFunction="TextBoxQualification"
    OnServerValidate="TextBoxQualification_Validate" Display="Dynamic" >
</asp:CustomValidator>

The client side validation function would be like this: 客户端验证功能将如下所示:

function TextBoxQualification(sender, args) {
    if(your condition) //that if you want to validate the textbox or not based on the selection of dropdown.
    {
        args.IsValid = false;    
    }
}

Same function you can write on server side also if you want. 如果需要,您也可以在服务器端编写相同的功能。 It is good to have validations on client and server both the sides, but if you don't want to validate on server side then also it's fine. 在客户端和服务器双方都进行验证是很好的,但是如果您不想在服务器端进行验证,那也很好。

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

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