Validation message error not working properly - c#

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>

Related

CompareValidator not working well - ASP.net

I have used compare validator to check whether the selected date is valid or it. The issue here is it only fires up when the submit button is clicked, is it possible to check when the user selects the date.
<tr id="trow" runat="server">
<td class="auto-style3">Need Duration</td>
<td class="auto-style2">
<asp:TextBox ID="TextBox1" runat="server" ReadOnly = "true"></asp:TextBox>
<asp:ImageButton ID="imgJoin" runat="server" ImageUrl="Images/calender.png"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox1" ErrorMessage="*" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator></td>
<td>
<asp:TextBox ID="TextBox2" runat="server" ReadOnly = "true"></asp:TextBox>
<asp:ImageButton ID="imgHide" runat="server" ImageUrl="Images/calender.png"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="TextBox2" ErrorMessage="*" ForeColor="Red"
SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1" runat="server" Operator="GreaterThanEqual"
ControlToValidate="TextBox2" ControlToCompare="TextBox1"
ErrorMessage='Invalid Date'
ForeColor="Red"></asp:CompareValidator>
</td>
</tr>
It has been a while, but I think you need to enable client side validation scripts by adding:
EnableClientScript="True"
Example
<asp:CompareValidator ID="CompareValidator1" EnableClientScript="True" runat="server"
Operator="GreaterThanEqual"
ControlToValidate="TextBox2" ControlToCompare="TextBox1"
ErrorMessage='Invalid Date'
ForeColor="Red"></asp:CompareValidator>
It's documented at msdn.
Aditionally, I do know that custom validators often lack a correct implementation of the javascript. I am not sure how the CompareValidatorbehaves in that sense.
You might need to create a inherited class, to implement the scripts fully. Before going there, try do research a bit.
For example, here is a solution with a custom validator

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.

ASP compareValidator's error message won't disappear after a valid input in entered

I have a form for adding some values to my DB. One of the input requires a Integer value, so on that TextBox I have two validators, an RequiredFieldValidator and an CompareValidator. The problem is that when I click on the input the first time ( or I got there with tab) the error message is displayed an it will never disappear, even if I enter a valid input.
<asp:Label ID="label4" runat="server" Text="label4"></asp:Label>
<asp:TextBox ID="textBox4" runat="server" style="width: 170px; margin: 5px 0;"></asp:TextBox>
<asp:RequiredFieldValidator ID="requiredFieldValidator4" runat="server"
ErrorMessage="*" ControlToValidate="textBox4" Display="Dynamic"
ForeColor="Red" ValidationGroup="1"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="compareValidator4" runat="server"
ErrorMessage="*" ControlToValidate="textBox4"
Type="Integer" Operator="DataTypeCheck" Display="Dynamic"
ForeColor="Red" ValidationGroup="1">
</asp:CompareValidator>
Above is my code for that input.
Have you already tried adding the property "ControlToCompare" to your compare validator?, for reference take a look of this post: http://forums.asp.net/t/1842937.aspx?CompareValidator+doesn+t+disappear+when+entering+the+correct+value

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" ...

Categories