ASP.NET Required Field Validator not working
Asked Answered
A

9

9

Hi all I need a required field validator for my textbox..This is my textbox..

<asp:TextBox ID="txtTimeSlotGroupName" runat="server" AutoPostBack="false" 
     ClientIDMode="Static"></asp:TextBox>  
<font color="red">*</font>  
<asp:RequiredFieldValidator ID="RequiredFieldValidator_txtTimeSlotGroupName"
     runat="server" ControlToValidate="txtTimeSlotGroupName" Display="None"
     ErrorMessage="Timeslot Group Required!" ForeColor="Red" InitialValue="0"
     ValidationGroup="TimeSlot"></asp:RequiredFieldValidator>

My button:

<asp:Button ID="btnAddTimeSlots" Text="Add Timeslots" CssClass="button" 
     runat="server" OnClick="btnAddTimeslots_Click" ValidationGroup="TimeSlot" 
     OnClientClick="javascript:shouldsubmit=true;"/>

I am not getting the error message. Any solutions?

Adolescent answered 25/4, 2013 at 5:31 Comment(1)
Need to put ValidationGroup to txtTimeSlotGroupName :)Coarsegrained
L
23

You have to define the Validation Group Of your Textbox too....to make it work

   <asp:TextBox ID="txtTimeSlotGroupName" runat="server" 
        AutoPostBack="false" ValidationGroup="TimeSlot" ClientIDMode="Static"></asp:TextBox>
Laraelaraine answered 25/4, 2013 at 5:35 Comment(0)
T
9

Remove InitialValue="0" from the RequiredFieldValidator tag, it is not required when you are validating the textbox.

Truant answered 25/4, 2013 at 5:34 Comment(0)
T
7

Even I was facing the same issue. Kindly check if any javascript are present on your page. Irrespective of above make use of Page.Validate() method and if(Page.IsValid) in your code. This will automatically force your validation controls and issue will be solved

Trucker answered 17/6, 2014 at 10:44 Comment(0)
C
3

If two objects have the same id the required field validator Does not work.

Canady answered 8/8, 2014 at 12:46 Comment(3)
Can you clarify? "Objects" do not have an "id" - do you mean names for variables? If so, from which context? Or, perhaps you mean the "id" attribute of an element? If so, which elements? Do you mean any elements on the page with the same id will break validation? I doubt that, but that's the closest I can come to interpreting this answer. Thank you in advance for any additional detail you can provide!!Exhibit
Late to the party but CHEGNI means any element on the page. A text box, a required field validator, a buton, a custom validator, ANYTHING. Any two elements that have id="idname". if any two have the same ID then some things can go wrong, especially in javascript.Kuehl
@Canady is right? any solution for this after 6 yearsVariform
D
1

You just add ValidationGroup="TimeSlot" in textbox

    <asp:TextBox ID="txtTimeSlotGroupName" runat="server" AutoPostBack="false" 
   ValidationGroup="TimeSlot"   ClientIDMode="Static"></asp:TextBox>
Dickdicken answered 8/4, 2015 at 17:21 Comment(0)
K
1

I had this same issue... but none of the above answers were the fix for me...

My problem was that I was missing the Page.isValid in my button press method. Below is my button code and the method called by the button.

Button:

<asp:Button ID="btnBtmSave" runat="server" Text="Save" OnClick="btnSave_Click" BtnGroup="save" TabIndex="18" />

Button method:

protected void btnSave_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        //Logic goes here
    }
}
Kunzite answered 6/11, 2018 at 20:19 Comment(0)
B
0

make the same Validation Group Of all your text and Add button and Validation

   ValidationGroup="AAA" 

and add the code to your save button:

  If (Page.IsValid) Then
        YOURSQL.Insert()
   'or ur code here'
    End If
Backchat answered 25/10, 2016 at 14:18 Comment(0)
D
0

In my case, For button, I was using both client side validation i.e onClientClick="return validate()", and ASP.NET Validation i.e Reguired field Validation (ValidationGroup). Hence the Required field validators were not firing.

Dunne answered 7/7, 2020 at 16:46 Comment(0)
K
0

firstly go to config file and add code after the</system.we>

    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
Kenelm answered 21/7, 2024 at 8:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.