Validate time within a range - asp.net

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

Related

Using ID as a rangevalidator value ASP

I've been working on a small game and I'm stuck trying to figure out how I can convert an ID that a user enters to become an integer value. I'm wanting to create a rangeValidator that specifies that a bet entered by a user can only go from 0 to the total amount of money they currently have.
current money: <asp:label ID="currentMoney" runat="server" Text=""></asp:label> <br />
Place bet: <asp:textbox ID="bet" runat="server"></asp:textbox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" ForeColor="Red" ControlToValidate="bet" runat="server" ErrorMessage="bet must be less than or equal to current money"> </asp:RequiredFieldValidator>
<asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="bet" type="Integer" MinimumValue="1" MaximumValue="currentMoney" ></asp:RangeValidator>
I'm not sure if there is a way to convert the currentMoney ID which gets originally entered by a user at an earlier part of my code to be used as the maximum value for my range validator. Is there any suggestions anyone might have?
You'd have to set the MaximumValue property in code:
RangeValidator2.MaximumValue = currentMoney.Text
As long as everything happens on the server. If not, then you'd have to update the client API; if you do that, when a postback happens, you'd have to update on the server or update on the client again (as these are not sent back to the server). I'm not quite sure how to do that, but googling should provide a solution.

Display Local Time in GridView

I have an asp.net gridview bound to the following sql server 2008r2 data source:
<asp:SqlDataSource ID="dsFault" runat="server" DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:ESBExceptionDb %>"
SelectCommand="select * from Fault order by datetime desc"></asp:SqlDataSource>
In my Fault table there is a column called DateTime of type datetime. The values stored in this column are in UTC format and I need them to be displayed in the local timezone for the browser
I have added a template field to the Columns collection of the grid view as follows:
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblLocalTime" runat="server"
Text='<%# String.Format("{0:f}", Eval("DateTime").ToLocalTime()) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
When I browse the page I get the following error:
CS1061: 'object' does not contain a definition for 'ToLocalTime' and no
extension method 'ToLocalTime' accepting a first argument of type 'object'
could be found (are you missing a using directive or an assembly reference?)
Can anyone please tell me where I've gone wrong?
Thanks, Rob.
The Eval("DateTime") Value that returns from your database is not a C# DataTime object.
and because the function .ToLocalTime() belongs to the DateTime c# object you can't use it.
You need to convert the object to string and then use the function .ToLocalTime()
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblLocalTime" runat="server"
Text='<%# Convert.ToDateTime(Eval("DateTime")).ToLocalTime() %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
Ones you converted it to DateTime Object, you can use any format available
For Example
Text='<%# Convert.ToDateTime(Eval("DateTime")).ToString("dd/MM/yyyy") %>'
In addition to the other answers, remember that ToLocalTime() will return the SERVER local time not the browser local time as you requested.
Try like this
<%# Eval("DateTime", "{0:T}")%>
<asp:Label ID="lblLocalTime" runat="server"
Text='<%# Eval("DateTime", "{0:T}")%>'>
</asp:Label>
This will do the trick! Give this a try.
<asp:TemplateField HeaderText="Account Created">
<ItemTemplate>
<asp:Label ID="lblLocalTime" runat="server"
Text='<%# Convert.ToDateTime(Eval("CreateDate", "{0:g}")).ToLocalTime() %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
It looks like there is no clean and simple way to do this, strange!!. The answer's mentioned so far try to convert the date to server timezone, not to client/browser's. i.e if the server is in USA and client in India, you won't see the datetime in indian timezone, it will be in USA's.
A duplicate of this has better answers. Basically, you either have to do one of below -
Send client timezone information to the server and do approp conversion there. It is better to send the timezone name rather than timezone offset as timezone offset can differ based on DST. You can use Intl.DateTimeFormat().resolvedOptions().timeZone to send the timezone name.
Send UTC date strings to
the client and let it do the conversion using javascript(new
Date(ISOUTCDateString)). This is what happens in WCF.

how to validate MaskedEditExtender control for MM/dd/yyyy format?

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>

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