RequiredFieldValidator not working on Button click in ASP.Net

Last Reply on Sep 12, 2017 11:51 PM By AnandM

Posted on Sep 12, 2017 11:43 PM

i have a aspx page having asp controls with requiredfiledvalidator when i press button it does not validate the controls

here is my code

<style type="text/css">
    input[type="file"]
    {
        display: block;
    }
    .imageThumb
    {
        max-height: 150px;
        border: 2px solid;
        margin: 10px 10px 0 0;
        padding: 1px;
    }
</style>
<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        if (window.File && window.FileList && window.FileReader) {
            $("#files").on("change", function (e) {
                var files = e.target.files,
                filesLength = files.length;
                for (var i = 0; i < filesLength; i++) {
                    var f = files[i]
                    var fileReader = new FileReader();
                    fileReader.onload = (function (e) {
                        var file = e.target;
                        $("<img></img>", {
                            class: "imageThumb",
                            src: e.target.result,
                            title: file.name
                        }).insertAfter("#files");
                    });
                    fileReader.readAsDataURL(f);
                }
            });
        } else { alert("Your browser doesn't support to File API") }
    });
</script>
<div>
    <ul class="breadcrumb">
        <li><a href="Addmission.aspx"><i class="icon-th-list"></i>Admission List</a></li>&nbsp;&nbsp;
        <li><a href="addAddmission.aspx"><i class="icon-plus"></i>New Admission</a></li>&nbsp;&nbsp;
        <li><a href="SearchAddmission.aspx"><i class="icon-search"></i>Search Admission</a></li>&nbsp;&nbsp;
        <li><a href="UpDateAddmission.aspx"><i class="icon-edit"></i>Edit Admission</a></li>
    </ul>
</div>
<div class="row-fluid sortable">
    <div class="box span12">
        <div class="box-header well" data-original-title>
            <h2>
                <i class="icon-edit"></i>Add New Addmission</h2>
        </div>
        <div class="box-content">
            <form id="Form2" class="form-horizontal">
            <fieldset>
                <div class="span6">
                    <div class="control-group">
                        <label class="control-label">
                            Admission No :</label>
                        <div class="controls">
                            <div class="input-append">
                                <asp:TextBox ID="txtaddno" runat="server" CssClass="input-medium focused" ValidationGroup="formvalidate"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Admission No is Required !"
                                    ControlToValidate="txtaddno" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                            </div>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="txtAddNo">
                            Student ID :
                        </label>
                        <div class="controls">
                            <asp:TextBox ID="txtstid" runat="server" CssClass="input-Medium uneditable-input"
                                Enabled="False"></asp:TextBox>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="TxtJdate">
                            Admission Date :<br />
                            (MM/DD/YYYY)</label>
                        <div class="controls">
                            <asp:TextBox ID="TxtJdate" runat="server" CssClass="input-Medium datepicker" TextMode="Date"
                                ValidationGroup="formvalidate"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Admission Date is Required !"
                                ControlToValidate="TxtJdate" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="TxtStuName">
                            Student Name :
                        </label>
                        <div class="controls">
                            <asp:TextBox ID="TxtStuName" runat="server" CssClass="input-Medium focused" ValidationGroup="formvalidate"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Student Name is Required !"
                                ControlToValidate="TxtStuName" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="TxtDOB">
                            Date Of Birth :<br />
                            (MM/DD/YYYY)</label>
                        <div class="controls">
                            <asp:TextBox ID="TxtDOB" runat="server" CssClass="input-Medium datepicker" ValidationGroup="formvalidate"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Date Of Birth is Required !"
                                ControlToValidate="TxtDOB" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label">
                            Gender :&nbsp;&nbsp;&nbsp;&nbsp;</label>
                        <div class="controls">
                            <asp:RadioButton ID="RBMale" runat="server" Checked="True" GroupName="Gender" />
                            Male
                            <asp:RadioButton ID="RBFeMale" runat="server" GroupName="Gender" />
                            Female
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="selectError3">
                            Class / standard:
                        </label>
                        <div class="controls">
                            <asp:DropDownList ID="DDLClass" runat="server" ValidationGroup="formvalidate">
                            </asp:DropDownList>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" InitialValue="Select"
                                ErrorMessage="Please Select Class" ControlToValidate="DDLClass" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                        </div>
                    </div>
                </div>
                <div class="span6">
                    <div class="control-group">
                        <label class="control-label" for="TxtFname">
                            Father's Name :
                        </label>
                        <div class="controls">
                            <asp:TextBox ID="TxtFname" runat="server" CssClass="input-Medium focused" ValidationGroup="formvalidate"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="Father's Name is Required !"
                                ControlToValidate="TxtFname" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="TxtMName">
                            Mother's Name :
                        </label>
                        <div class="controls">
                            <asp:TextBox ID="TxtMName" runat="server" CssClass="input-Medium focused" ValidationGroup="formvalidate"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="Mother's Name is Required !"
                                ControlToValidate="TxtMName" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="txtPContact">
                            Parents Contact No :
                        </label>
                        <div class="controls">
                            <asp:TextBox ID="txtPContact" runat="server" CssClass="input-Medium focused" ValidationGroup="formvalidate"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ErrorMessage="Parents Contact No is Required !"
                                ControlToValidate="txtPContact" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="txtemail">
                            Parents Email Id :
                        </label>
                        <div class="controls">
                            <asp:TextBox ID="txtemail" runat="server" CssClass="input-Medium focused" ValidationGroup="formvalidate"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ErrorMessage="Email Id is Required !"
                                ControlToValidate="txtemail" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="eg. you@domain.com"
                                ControlToValidate="txtemail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="txtAdd">
                            Address :
                        </label>
                        <div class="controls">
                            <asp:TextBox ID="txtAdd" runat="server" CssClass="input-Medium focused" TextMode="MultiLine"
                                ValidationGroup="formvalidate"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ErrorMessage="Address is Required !"
                                ControlToValidate="txtAdd" ValidationGroup="formvalidate"></asp:RequiredFieldValidator>
                        </div>
                    </div>
                </div>
                <div class="span11">
                    <div class="control-group">
                        <label class="control-label" for="txtAdd">
                            Photographs :
                        </label>
                        <div class="controls">
                            <input type="file" id="files" name="files[]" multiple />
                        </div>
                    </div>
                </div>
            </fieldset>
            </form>
            <asp:Label ID="lblerror" runat="server" Text=""></asp:Label>
            <div class="form-actions">
                <asp:Button CssClass="btn btn-primary" ID="BtnAddMore" runat="server" Text="Add More" />
                <asp:Button CssClass="btn btn-primary" ID="BtnSAdd" runat="server" Text="Save Addmission" />
                <asp:Button CssClass="btn" ID="BtnRAdd" runat="server" Text="Reset" />
            </div>
        </div>
    </div>
</div>
Posted on Sep 12, 2017 11:51 PM
yogeshc says:
<asp:Button CssClass ="btn btn-primary" ID="BtnSAdd" runat="server" Text="Save Addmission" />

 you havent assigned ValidationGroup to the above button thats why your validation is not working.

Replace above code with below.

<asp:Button CssClass="btn btn-primary" ID="BtnSAdd" ValidationGroup="formvalidate"
     runat="server" Text="Save Addmission" />