How to create custom control tool for textbox validation? - c#

I want to create a tool like textbox validation so how to create textbox validate tool in asp.net c#

<asp:Textbox id="txtLastName" runat="server"></asp:Textbox>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server"
ControlToValidate="txtLastName"
ErrorMessage="Last name is a required field."
ForeColor="Red">
</asp:RequiredFieldValidator>

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

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.

Required attribute in ASP Control

I want to know how to use required attribute in asp.net. .In html I used required attribute for mandatory fields. How to use it in asp .net?
I tried this so far, is it correct?
<p class="field-wrapper required-field">
<label>First Name</label>
<asp:TextBox ID="TextBox1" name="f_name" runat="server" required>
</asp:TextBox>
</p>
there's no required attribute. you have to use a RequiredFieldValidator
<asp:TextBox ID="TextBox1" name="f_name" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="TextBox1" errormessage="Please enter a value!" />

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