date validation in asp.net - asp.net

is this following a proper date validation fn
<asp:TextBox ID="date" Width="80px" MaxLength="10" runat="server" />
</td>
<td>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="date"
ErrorMessage="date. " ValidationExpression="^(\d{4})(\d{2})(\d{2})$" Display="Static" Font-Names="Arial"
Font-Size="11" runat="server">
enter a valid date formate
</asp:RegularExpressionValidator>

I believe this would be a better approach, use what's built-in:
<asp:TextBox ID="date" Width="80px" MaxLength="10" runat="server" />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="date" ErrorMessage="* Enter a valid date"
Operator="DataTypeCheck" Type="Date" />
The key settings are Operator and Type.
Note if you have to consider multiple cultures and such, this becomes a much more involved question, hopefully that isn't the case.

Related

How to make a extender calendar unable to select a date base on a selected date?

How can I make user unable to select an end date before the an start date?
<asp:Label ID="lblStartDate" runat="server" Text="Start Date: " CssClass="labelClass"></asp:Label>
<asp:TextBox ID="tbStartDate" runat="server" ReadOnly="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalExtStartDate" runat="server" Format="dd/MM/yyyy" TargetControlID="tbStartDate" />
<asp:Label ID="lblEndDate" runat="server" Text="End Date: " CssClass="labelClass"></asp:Label>
<asp:TextBox ID="tbEndDate" runat="server" ReadOnly="True"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="CalExtEndDate" runat="server" TargetControlID="tbEndDate" Format="dd/MM/yyyy" />
In my .aspx.cs I've got but it still doesn't work.
if (!IsPostBack)
{
CalExtStartDate.StartDate = DateTime.Now.AddDays(-7);
CalExtEndDate.StartDate = CalExtStartDate.SelectedDate;
}
I believe that you can use a compare validator paired with 2 required field validators (one for each date textbox):
<asp:TextBox ID="tbStartDate" runat="server" ReadOnly="True"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="End Date required" ID="requiredDate1" ControlToValidate="tbStartDate" />
<asp:TextBox ID="tbEndDate" runat="server" ReadOnly="True"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Start Date required" ID="requiredDate2" ControlToValidate="tbEndDate" />
<asp:CompareValidator runat="server" ID="compareValidator1" ErrorMessage="End date must be after start date" ControlToCompare="tbStartDate" ControlToValidate="tbEndDate" Operator="GreaterThan" Type="Date" />

Validate end date should be greater than start date?

datePicker.ascx
<asp:TextBox ID="txtDatePicker" runat="server"></asp:TextBox>
<asp:ImageButton runat="Server" ID="Image1" ImageUrl="~/Images/calendar.png" AlternateText="Click to show calendar" />
<ajaxToolkit:CalendarExtender Format="dd/MM/yyyy" ID="CalendarExtender1" TargetControlID="txtDatePicker" runat="server" PopupButtonID="Image1" />
register.aspx
<%# Register Src="~/Modules/datePicker.ascx" TagName ="datePicker" TagPrefix ="uc1" %>
Start Date <uc1:datePicker ID="datePickerSD" runat="server" >
End Date <uc1:datePicker ID="datePickerED" runat="server" />
<asp:CustomValidator ID="CustomValidator1" ControlToValidate="datePickerED"
ErrorMessage="End date should be greater than start date" runat="server" />
Tried to get clientid of start date..but confused..
<asp:CompareValidator ID="cmpr" runat="server" ControlToCompare ="txtDate1" ControlToValidate="txtDate2" Operator="GreaterThan" Type="Date" ErrorMessage="To date should great than small data"></asp:CompareValidator>
Have you tried compare validator ? This can help you
Solved :
<asp:CompareValidator ID="compareValidatorDate" runat="server" ControlToValidate="datePickerED$txtDatePicker" ControlToCompare ="datePickerSD$txtDatePicker" CultureInvariantValues="true"
ErrorMessage="End date should be greater than start date" Operator="GreaterThanEqual" SetFocusOnError="false" Type="String" Display="Dynamic" EnableClientScript="true" Font-Size="Small" ForeColor="Red"></asp:CompareValidator>

I want validation expression for required field validator and the numeric validator together for phone number

I want validation expression for required field validator and the numeric validator together
for phone number
<asp:TextBox ID="txtPhonenumber" runat="server" MaxLength="11" TabIndex="11" onKeyPress="return validateNumbersOnly();" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Enter Phone Number" ControlToValidate="txtPhonenumber" ></asp:RequiredFieldValidator>
this code works only if field is empty
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPhonenumber" ErrorMessage="Only Numbers" Display="None" ValidationExpression="^\d+$" ></asp:RegularExpressionValidator>
ValidationExpression="^\d+$"
use this for numeric format validation
<asp:TextBox ID="txtPhonenumber" runat="server" MaxLength="11" ></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Phone Number" ControlToValidate="txtPhonenumber" ></asp:RequiredFieldValidator><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtPhonenumber" ErrorMessage="Only Numbers" Display="None" ValidationExpression="^\d+$" ></asp:RegularExpressionValidator>

Input parameter 'controlId' cannot be an empty string when using validator inside formview

I have got a FormView where I have several fields. Those fields should be validated by asp.net validators, like this:
<asp:FormView ID="OrderDetails" runat="server" DataKeyNames="ID" DataSourceID="DS_Order" DefaultMode="Edit" AutoGenerateRows="false" ClientIDMode="Static" RenderOuterTable="false">
<EditItemTemplate>
<asp:TextBox ID="EinkaufsauftragsNr" runat="server" ClientIDMode="Static" Text='<%# Bind("EinkaufsauftragsNr") %>'/>
<asp:CompareValidator runat="server" Operator="DataTypeCheck" Type="Integer" ValidationGroup="EditValidation" Display="Dynamic"
ControlToValidate="EinkaufsauftragsNr" ErrorMessage="Nur Ganzzahlen erlaubt." CssClass="validator" />
.....
</EditItemTemplate>
</FormView>
But when I execute this, I get: "Input parameter 'controlId' cannot be an empty string."
When I remove the validator, the error-msg disappears as well.
After searching for this problem on the net, I thought the problem was that the control cannot be found inside the formview, so I put the clientidmode="static" there -> but that didn't solve my problem.
Do you have any other suggestions on how I could solve this one?
You did not assign id to CompareValidator Control, You have to assign ID to asp:CompareValidator
Change
<asp:CompareValidator runat="server" Operator="DataTypeCheck"
Type="Integer" ValidationGroup="EditValidation" Display="Dynamic"
ControlToValidate="EinkaufsauftragsNr" ErrorMessage="Nur Ganzzahlen erlaubt."
CssClass="validator" />
To
<asp:CompareValidator ID="SomeID" runat="server" Operator="DataTypeCheck"
Type="Integer" ValidationGroup="EditValidation" Display="Dynamic"
ControlToValidate="EinkaufsauftragsNr" ErrorMessage="Nur Ganzzahlen erlaubt."
CssClass="validator" />

How to speed up a RegularExpressionValidator in asp.net?

I have a text box that is using a RegularExpressionValidator to check that a string is in the correct format, and it is taking roughly 20 seconds to check. Is there a way to speed it up?
<asp:TextBox runat="server" ID="txtNumber"
width="9 em"
text='<%#Eval("strNumber")%>' AutoPostBack="true"
MaxLength="16" ontextchanged="txtNumber_TextChanged" />
<asp:RegularExpressionValidator
ID="NumberLength"
runat="server"
ValidationExpression="^\S{13,16}$"
ErrorMessage="Invalid Number"
Display="None" ControlToValidate="txtNumber"/>
<ajax:ValidatorCalloutExtender
ID="ValidatorCalloutExtender1"
runat="server" TargetControlID="NumberLength">
</ajax:ValidatorCalloutExtender>

Resources