MusicLovingIndianGirl
MusicLovingIndianGirl

Reputation: 5947

ASP.NET Required Field Validator not working

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?

Upvotes: 9

Views: 73387

Answers (10)

esims
esims

Reputation: 420

In my case it didn't work because the HTML of the container was modified by javascript (jQuery actually).

Some answers mention the use of the ValidationGroup attribute, but I don't think that is relevant to the original question, because it is not a required attribute and it only restricts the buttons that perform validation.

Upvotes: 0

user26452728
user26452728

Reputation: 1

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

    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>

Upvotes: 0

Surendra
Surendra

Reputation: 45

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.

Upvotes: 0

AFatBunny
AFatBunny

Reputation: 71

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
    }
}

Upvotes: 1

Aladein
Aladein

Reputation: 312

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

Upvotes: 0

Hema Shetty
Hema Shetty

Reputation: 109

You just add ValidationGroup="TimeSlot" in textbox

    <asp:TextBox ID="txtTimeSlotGroupName" runat="server" AutoPostBack="false" 
   ValidationGroup="TimeSlot"   ClientIDMode="Static"></asp:TextBox>

Upvotes: 1

CHEGENI
CHEGENI

Reputation: 31

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

Upvotes: 3

Prashant Banavali
Prashant Banavali

Reputation: 71

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

Upvotes: 7

Amit Singh
Amit Singh

Reputation: 8109

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>

Upvotes: 23

yalinhahs
yalinhahs

Reputation: 121

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

Upvotes: 9

Related Questions