RequiredFieldValidator not working for tow Dropdownlist - c#

I have tow Dropdownlist in my web page as follows.
<asp:DropDownList ID="JobDDL" runat="server" class="form-control" ValidationGroup="gg" >
</asp:DropDownList>
<asp:RequiredFieldValidator InitialValue="-1" ID="Req_ID" Display="Dynamic"
ValidationGroup="gg" runat="server" ControlToValidate="JobDDL"
Text="*" ErrorMessage="أختر الفرع من فضلك"></asp:RequiredFieldValidator>
<asp:DropDownList ID="BranchDDL" runat="server" class="form-control">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="BranchDDL" ErrorMessage="أختر الفرع من فضلك" InitialValue="اختر من فضلك" ForeColor="red"></asp:RequiredFieldValidator>
frist Dropdownlist RequiredFieldValidator (ID="JobDDL") not work . The second RequiredFieldValidatorworks(ID="BranchDDL")

Please add Display="Dynamic" and Initial value="Select" in Required Field Validator
if your dropdown list first element has select values please Add Initial Value="Select" in Required Field Validator.
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="DropDownList1" ErrorMessage="Please Select Type" InitialValue="--Select--" Font-Bold="True" ForeColor="#CC0000" Display="Dynamic"></asp:RequiredFieldValidator>

Related

Validation message error not working properly

I'm having a problem with validation message errors for web forms. I need to get an error when both statements are true(both fields are empty). When I change statement && for || I'm able to get an error but that's not what I want.Thank you. Here is my C# code
protected void CustomValidatorForm_ServerValidate(object source, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(drpState.Text) && string.IsNullOrEmpty(txtRegion.Text))
args.IsValid = false;
else
{
args.IsValid = true;
}
}
I'm Trying to run this code for my forms:
<asp:DropDownList ID="drpState" runat="server" CausesValidation="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtRegion" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</div>
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
<asp:CustomValidator ID="CustomValidatorForm" runat="server"
ControlToValidate ="txtRegion" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
try using like this ,
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ErrorMessage="*" ControlToValidate="txtFrom" runat="server" Display="Dynamic" ForeColor="Red" />
Easy way to do this,
After you create a <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
type <asp:RequiredFieldValidator and press Tab Button twice,
You can also give any ErrorMessage as well as define color of the ErrorMessage
It turns out I just needed to add this property ValidateEmptyText="True" and set it to true. This is how validator should look like:
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red" ValidateEmptyText="True">
</asp:CustomValidator>

ASP.NET: How can I "display: none" ASP validators when not required?

I have the following asp code:
<asp:TextBox ID="txtNewPassword" TextMode="Password" Placeholder="New Password" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorNewPassword"
runat="server" ErrorMessage="New Password required" ControlToValidate="txtNewPassword" ForeColor="Red">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ErrorMessage="Password must be at least 6 characters long" ForeColor="Red" ControlToValidate="txtNewPassword"
ValidationExpression="[0-9a-zA-Z]{6,}"/>
The thing is that they are hidden in visibiliy and not in display when not required, but that is a problem because they take space nonetheless.

How to make required field validator on dropdownlist?

I have a dropdownlist where I added an extra value to (Choose channel...). If this value is selected I want a message displayed that is required, what I tried is:
<td>
<asp:DropDownList ID="ServiceChannelDropDownList" AppendDataBoundItems="true"
runat="server" DataTextField="Description" DataValueField="ID">
<asp:ListItem Value="-1" Text="Choose channel..." />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="DropDownListRequiredFieldValidator" runat="server"
ControlToValidate="ServiceChannelDropDownList"
InitialValue="-1"
ErrorMessage="*"
</asp:RequiredFieldValidator>
</td>
Unfortunately this just let me select the initial value (which naturally does not exist in the database) without saying it is not allowed. How to solve this?
You need to add the default value in your code behind(.cs) where you bind Dropdownlist data, instead of doing it in .aspx page
ServiceChannelDropDownList.DataBind();
ServiceChannelDropDownList.Items.Insert(0, new ListItem("Choose channel...","-1"));
<td>
<asp:DropDownList ID="ServiceChannelDropDownList" AppendDataBoundItems="true"
runat="server" DataTextField="Description" DataValueField="ID">
<asp:ListItem Value="0" Text="Choose channel..." />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="DropDownListRequiredFieldValidator" runat="server"
ControlToValidate="ServiceChannelDropDownList"
InitialValue="0"
ErrorMessage="*"
</asp:RequiredFieldValidator>
</td>
I needed to add a ValidationGroup to the RequiredFieldValidator, e.g.,
<asp:RequiredFieldValidator ID="DropDownListRequiredFieldValidator" runat="server"
ControlToValidate="ServiceChannelDropDownList"
InitialValue="0"
ErrorMessage="Channel is required."
ValidationGroup="WorkflowValidation"
CssClass="ValidationCss"
EnableTheming="false">*
</asp:RequiredFieldValidator>
Thanks all for the help!

how to put focus back on textbox which fails validation ,inside gridview

I have below textbox inside itemtemplate of gridview..problem is when i click on edit button of gridview,and if i enter invalid value in textbox according to validation logic, focus of textbox is lost when i click on update button..if i am at 30est row focus went to top most row..how to prevent focus..
<asp:TextBox ID="tbattendence" Width="40px" runat="Server" Text='<%# Eval("attendence") %>' onkeydown = "return (event.keyCode!=13);">
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tbattendence"
ErrorMessage="Attended Attendence is required!" Display="Dynamic" ValidationGroup="bottom"
ForeColor="#6600FF">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator" runat="server" ErrorMessage="Attended Attendence must be Lesser!"
ControlToValidate="tbattendence" ControlToCompare="tbcutoff"
Display="Dynamic" Operator="LessThanEqual" Type="Integer" ValidationGroup="bottom">*</asp:CompareValidator>
Add
SetFocusOnError="true"
to your validation controls like this
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" SetFocusOnError="true" ...

Validation Group used to validate the group not show the error message

In the form in aspx I have two textbox and one image button for each textbox.
I need validation the value for each textbox in a separate way and for this I have for each image button linked to a different event.
For this I have finded in google and I have tried this tutorial:
http://www.c-sharpcorner.com/Blogs/3625/use-of-validation-group-in-Asp-Net.aspx
But in my form the Validation Group in asp.net not working and I don't understand the reason.
What does not work are the warning messages that indicate required fields.
What's wrong?
My code aspx below, thank you in advance.
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server" width="100" cssclass="ddl_Class" validationgroup="First"></asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" controltovalidate="TextBox1"
errormessage="Error in TextBox1" text="***" display="None" validationgroup="First"></asp:requiredfieldvalidator>
<asp:regularexpressionvalidator id="RegularExpressionValidator4" runat="server" controltovalidate="TextBox1"
errormessage="TextBox1 only number" text="***" display="None" validationexpression="^\d+$" validationgroup="First"></asp:regularexpressionvalidator>
<asp:imagebutton id="btnSave1" runat="server" validationgroup="First" onclick="ButtonSave1_Click" imageurl="/Images/save_button.gif" onclientclick="if (!confirm('Confirm?')) return false;" />
<asp:textbox id="TextBox2" runat="server" width="100" cssclass="ddl_Class" validationgroup="Second"></asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator4" runat="server" controltovalidate="TextBox2"
errormessage="Error " text="***" display="None" validationgroup="Second"></asp:requiredfieldvalidator>
<asp:imagebutton id="btnSave2" runat="server" validationgroup="Second" onclick="ButtonSave2_Click" imageurl="/Images/save_button.gif" onclientclick="if (!confirm('Confirm?')) return false;" />
</div>
<asp:validationsummary id="First" runat="Server" showmessagebox="true" cssclass="validation-summary-errors" />
<asp:validationsummary id="Second" runat="Server" showmessagebox="true" cssclass="validation-summary-errors" />
</form>
I have verified your code, only problem that I identified is missing validation group in validation summary tag.
See below:
<asp:validationsummary id="First" validationgroup="First" runat="Server" showmessagebox="true" cssclass="validation-summary-errors" />
<asp:validationsummary id="Second" validationgroup="Second" runat="Server" showmessagebox="true" cssclass="validation-summary-errors" />
Hope this will work

Categories