Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 988
MusicLovingIndianGirl Avatar asked Apr 25 '13 05:04

MusicLovingIndianGirl


4 Answers

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

like image 120
Prashant Banavali Avatar answered Oct 05 '22 09:10

Prashant Banavali


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>
like image 38
Amit Singh Avatar answered Oct 05 '22 10:10

Amit Singh


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

like image 44
CHEGENI Avatar answered Oct 05 '22 10:10

CHEGENI


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

like image 34
yalinhahs Avatar answered Oct 05 '22 10:10

yalinhahs