how to validate MaskedEditExtender control for MM/dd/yyyy format? - asp.net

I am using maskedEditExtender control for getting date..
Code inside MaskedEditExtender is
cc1:MaskedEditExtender ID="MaskedEditExtender1" runat="server" TargetControlID="ui_txtRequestDateTime" ClearMaskOnLostFocus="false" Mask="99/99/9999 99:99" UserTimeFormat ="TwentyFourHour">
I need DateTime in MM/dd/yyyy hh:mm formate ,If i use calenderExtender control i can use Format="MM/dd/yyyy" ,but here i couldt specify the format MM/dd/yyyy, its generally "99/99/9999 99:99" format, the 1st two 9's for month, how to tell to user that they have to give month only, if i give value above 12, like 23,24 its showing exception..
Please give some idea..

Use RegularExpression validator with your textbox as follows:
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="valRegDate" ValidationGroup="OrderAdd"
ControlToValidate="txtDate" runat="server" ErrorMessage="Please provide a valid date."
ForeColor="Red" ValidationExpression="^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$"></asp:RegularExpressionValidator>

Related

Validate time within a range

I have a TextBox and AjaxControlToolkit MaskedEditExtender for users to enter a time. The control is bound to a timespan property in code (and time(7) column in SQL Server). The requirement is to ensure that all times are between 1800h and 0000h (it's for bat recording). Can anyone suggest how to use the MaskedEditValidator to ensure the time entered is in this range?
My code so far:
<asp:TextBox ID="txtEmergenceTime" runat="server" CssClass="formInput"
Text='<%# Eval("EmergenceTime", "{0:hh\:mm}") %>' />
<asp:MaskedEditExtender ID="meeEmergenceTime" runat="server"
TargetControlID="txtEmergenceTime" Mask="99:99" MaskType="Time"
AutoComplete="false" AutoCompleteValue="0"/>
<asp:MaskedEditValidator ID="valEmergenceTime" runat="server"
ErrorMessage="Emergence time must be between 18:00 and 00:00"
CssClass="error" ControlToValidate="txtEmergenceTime"
ValidationGroup="roostCount">*</asp:MaskedEditValidator>
You may set interval from 18:00 to 23:59 and utilize MaskedEditValidato's MinimumValue and MaximumValue properties. Or you can define ClientValidationFunction property for validator extender and use custom validation

mm/dd/yyyy regex for Regular Expression Validator in ASP.Net failed to validate

I know such questions are in ton and it feels like a dupe question. As I haven't found the solution closed to me, thats why i am posting this. I have a regex in my regular exp validator
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Invalid date format. Valid dates are 12/31/2009 (mm/dd/yyyy). " ControlToValidate="txtDOB" Display="None" EnableTheming="False" EnableViewState="False" SetFocusOnError="True" ValidationGroup="grpUser"
ValidationExpression="^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$"></asp:RegularExpressionValidator>
Its failing to validate the date in mm/dd/yyyy or m/d/yyyy format. I have taken this from RegexLib where the author said its valid for both formats. Are the ^ and $ creating issues. Please help.
Unless you have a good reason to do otherwise, you should use a CompareValidator
<asp:CompareValidator Operator="DataTypeCheck" Type="Date" ...
This is working
^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/]\d{4}$
<asp:RegularExpressionValidator
ValidationExpression="^([0-9]|0[1-9]|1[012])\/([0-9]|0[1-9]|[12][0-9]|3[01])\/(19|20)\d\d$"
ControlToValidate="txtDOB" ErrorMessage="Invalid Format. Use MM/DD/YYYY" runat="server"
ValidationGroup="Password" CssClass="colorred">
</asp:RegularExpressionValidator>
this works for both m/d/yyyy and mm/dd/yyyy with valid dates

how to format date time at time of binding in gridview

I have a field of type DateTime which is bound in gridview.
Now, i would liketo display only date from this field, and not time.
Date Should be in 1/1/0001 this format.
I am using DotnetNuke
My code is as follows
<asp:TemplateField HeaderText="Created On">
<itemtemplate>
<%#DataBinder.Eval(Container.DataItem, "Alb_Created_Date","0:dd/MM/yyyy}")%>
</itemtemplate>
</asp:TemplateField>`.
I have tried the format that is used but its output is like this 1/1/0001 12:00:00 AM.
What shall I do?
You are missing an opening angular brace {. Try this.
<asp:TemplateField HeaderText="Created On">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "Alb_Created_Date", "{0:dd/MM/yyyy}")%>
</itemtemplate>
</asp:TemplateField>
In your code you have given wrong syntax in the format, and due to which its not formatting correctly and giving out the data base value itself. Please, format it as below.
<%#DataBinder.Eval(Container.DataItem, "Alb_Created_Date","{0:dd/MM/yyyy}")%>

ASP.NET: how to validate

I have a simple ASP.NET (VB) page with a CompareValidator. I can check to ensure the value is numeric, but I have no idea how to check for the length (business rules require a 7 digit number). Existing code is below:
<asp:TextBox ID="txtPolicyNo" runat="server"
BorderStyle="Ridge"></asp:TextBox>
<asp:CompareValidator ID="cvCheckPolicy" runat="server"
ErrorMessage="Must be a valid policy number" ControlToValidate="txtPolicyNo"
Type="Integer" Operator="DataTypeCheck">
</asp:CompareValidator>
How can I accomplish this?
Thanks,
Jason
Use a RegularExpressionValidator with a regex string like "^\d{7}$". That will ensure you get a 7 digit number. You might have to combine that with a RequiredFieldValidator to make sure they enter something.
To Check business rules, the CustomValidator is normally used.
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="invalid text"></asp:CustomValidator>
But if you only want to validate 7 digits, you can use RegularExpressionValidator with the validation expression: ValidationExpression=^\d{7}$
Use a RangeValidator or RegularExpressionValidator
<asp:RangeValidator id="rvCheckPolicy"
ControlToValidate="txtPolicyNo"
MinimumValue="1000000"
MaximumValue="9999999"
Type="Integer"
EnableClientScript="false"
ErrorMessage="Must be a valid policy number"
runat="server"/>
<asp:RegularExpressionValidator id="revCheckPolicy"
ControlToValidate="txtPolicyNo"
ValidationExpression="\d{7}"
ErrorMessage="Must be a valid policy number"
runat="server"/>

I need CompareValidator control to accept date in format month/day/year

I have the following template defined within DetailsView ( DetailsView is bound to object data source ):
<EditItemTemplate>
<asp:TextBox ID="txtReleaseDate" runat="server"
Text='<%# Bind("ReleaseDate", "{0:d}") %>'>
</asp:TextBox>
<asp:CompareValidator ID="valReleaseDateType" runat="server"
ControlToValidate="txtReleaseDate" Type="Date" Operator="DataTypeCheck"
Display="Dynamic" > *
</asp:CompareValidator>
</EditItemTemplate>
Assuming I enter into TextBox txtReleaseDate a date in format month/day/year, then upon clicking an Update or Insert button, a CompareValidator control complains that date format is not valid. But if I enter date in format day/month/year, then object data source throws an exception Cannot convert value of parameter 'releaseDate' from 'System.String' to 'System.DateTime', while CompareValidator doesn’t complain.
I need the two controls to accept the same date format, so:
a) Since my DB stores date in format day/month/year, the best option would be for ODS to also accept this as valid date format. Can ODS be configured that way?
b)Can CompareValidator be configured to also accept month/day/year format?
thanx
Probably you can set the CurrentUICulture of thread to en-GB format.
Try setting the web.config Globalization
I think
format is "dd/MM/yyyy"
try putting "eb-US" one.
Gud luck.

Resources