I want to disable future dates
This is what I am trying <Asp:TextBox ID="TextBox4" runat="server" TextMode="Date" ClientIDMode="Static"/>
I am expecting when user try to enter a date it will shows only current date not future date
Related
I working on with C# and ASP.NET webforms using Visual Studio 2022. I have an issue when trying to write time to a textbox.
When I try to write time 11:30 pm to a textbox, it is converted automatically to 23:30 - why was it converted?
I need time to working on 12 hours mode exactly as I write it.
So if I write 11:30 am, it must be shown as 11:30 am.
11:30 pm must be shown as 11:30 pm - here I have the issue that it is converted to 23:30 pm.
Expected result:
when I write 11:30 pm, it must show 11:30 pm
What do I do to solve this issue please?
webpage.cs
protected void txtFromTime_TextChanged(object sender, EventArgs e)
{
var Transactiondate = Convert.ToDateTime(txtDate.Text);
var dateonly = Transactiondate.ToString("dd/MM/yyyy");
var eventDate = txtDate.Text + " " +Convert.ToDateTime(txtFromTime.Text).ToString("HH:mm");
Label1.Text = eventDate.ToString();
}
web page.aspx
<form id="form1" runat="server">
<div>
<asp:Label ID="lblDate" runat="server" Text="Date" ></asp:Label>
<asp:TextBox ID="txtDate" runat="server" TextMode="Date" AutoPostBack = "True" OnTextChanged="txtDate_TextChanged"></asp:TextBox>
<asp:Label ID="lblFromTime" runat="server" Text="FromTime"></asp:Label>
<asp:TextBox ID="txtFromTime" runat="server" TextMode="Time" AutoPostBack = "True" OnTextChanged="txtFromTime_TextChanged"></asp:TextBox>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</form>
picture issue
It's because you used upper-case HH instead of lower-case hh in the format string. You also need the AM/PM indicator:
hh:mm tt
This is well-documented:
https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
Unfortunately, right now this always results in an uppercase indicator, so you'll see 11:30 PM. If you really need to see 11:30 pm you need to correct it with normal string manipulation.
I'd like to see the t ("t"ime of day) specifiers enhanced, such that lowercase t is a lowercase indicator and uppercase T is an uppercase indicator, along with a mechanism to infer the case from the system's datetime format. I suspect the reason this has not happened is a certain important time format uses a literal T to separate the date and time portions of the string.
why not consider just one text box, and use textmode=
So, say this text box:
<asp:TextBox ID="TextBox1" runat="server"
TextMode="DateTimeLocal">
</asp:TextBox>
You then get/see this:
In other words, you have one text box - it will have both date and time for you.
About the only thing I don't like, is tab key does not select, you have to hit enter key.
but, it also allows in-line editing if you don't want to use the picker.
Hence this:
And if you enter (without the picker), then you have to use arrow keys to move to the next part. Again, I don't find this 100% intuitive.
However, you can drop in 2 controls, and feed it the "one" date time variable. You feed using "iso" date format, but it will take on your local settings.
So, say this markup:
<h4>Enter Date</h4>
<asp:TextBox ID="txtDate" runat="server"
TextMode="Date">
</asp:TextBox>
<h4>Enter Time</h4>
<asp:TextBox ID="txtTime" runat="server"
TextMode="Time">
</asp:TextBox>
And code to load:
DateTime dtStart = DateTime.Now;
txtDate.Text = dtStart.ToString(#"yyyy-MM-dd");
txtTime.Text = dtStart.ToString(#"HH:mm");
NOTE close above, I feed the time a 24 hour format, but it does display in 12 hour format.
and now we get/see this:
<asp:TextBox ID="txtdob" TextMode="Date" CssClass="form-control" placeholder="DOB"
onfocus="(this.type='date')" runat="server"></asp:TextBox>
The placeholder date should be filled with the date of birth value.
I want to enter dates into my database using asp.net form-view but my challenge is that the dates once entered like 04 jun 2018, when I go to edit the template it will display as 4/06/2018 12:00:00 a.m.
So the system will crash if I hit update, I have learnt that this 4/06/2018 12:00:00 a.m. is the cause for crush as the server will respond with datatype mismatch.
How can I display the date in my text box in edit template like 04/06/2018 12:00:00 am as this is a valid date format?
It's working fine with local machine and the problem is when hosted it on Windows Server 2012.
Below is a textbox in my EditItemTemplate
ApplicDate:
<asp:TextBox
ID="ApplicDateTxtBox"
EnableViewState="true" runat="server"
Text='<%# Bind("ApplicDate") %>' TextMode="singleline" />
<asp:RegularExpressionValidator
ID="RegularExpressionValidator3"
runat="server"
ForeColor="red"
ErrorMessage="Incorrect date format: example 15 feb 2018!"
ValidationExpression="^([1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[-/.](19|20)\d\d\s\d{2}\:\d{2}\:\d{2}\s\w{1}\.\w{1}\.$|(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[-/.](19|20)\d\d\s\d{2}\:\d{2}\:\d{2}\s\w{1}\.\w{1}\.$|(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[-/.](19|20)\d\d\s\d{2}\:\d{2}\:\d{2}\s\w{2}$|(0[1-9]|[12][0-9]|3[01])\s\w{3}\s(19|20)\d\d$"
ControlToValidate="ApplicDateTxtBox">
</asp:RegularExpressionValidator>
You can set the date format with BoundField.DataFormatString
For your case, it should be formatted as G for
General date/time pattern (long time).
Specify this to FormView binding Field as
<%# Bind("<BINDING DATE COLUMN>", "{0:G}") %>
For your case,
<asp:TextBox ID="ApplicDateTxtBox" EnableViewState="true" runat="server" Text='<%# Bind("CheckDate", "{0:G}") %>' TextMode="singleline" />
I have a TextBox where I am taking input from as user as dd/mm/yyyy hh:mm:ss. Now, I want to validate it with a regular expression. I'm not sure how to apply the expression. I have attached my code as well.
<tr>
<td style="width: 30%" class="EcommLabel">
Date From
</td>
<td style="width: 70%" class="EcommLabel">
<asp:TextBox ID="txtDateFrom" CssClass="EcommNormalTextBox" runat="server">
</asp:TextBox>MM/DD/YYYY<br />
<%-- <asp:RegularExpressionValidator ID="regDateFrom" 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)))$" ControlToValidate="txtDateFrom" ValidationGroup="Promotion" runat="server" ErrorMessage="Invalid Date"></asp:RegularExpressionValidator>--%>
<asp:RangeValidator runat="server" ID="rvDateFrom" Type="Date" ControlToValidate="txtDateFrom" MaximumValue="3000/12/31" MinimumValue="2000/1/1" ErrorMessage="Invalid Date" Display="Dynamic" ValidationGroup="Promotion" />
</td>
</tr>
use this expression "(\d{2}):(\d{2}):(\d{4}):(\d{2}):(\d{2}):(\d{2})" like
<asp:RegularExpressionValidator ID="regDateFrom" ValidationExpression="(\d{2}):(\d{2}):(\d{4}):(\d{2}):(\d{2}):(\d{2})"
ControlToValidate="txtDateFrom" ValidationGroup="Promotion" runat="server"
ErrorMessage="Invalid Date"></asp:RegularExpressionValidator>
Also see the following stackoverflow questions:
How to write a regex for MM:DD:YYYY:HH:MM:SS
MM/DD/YYYY HH:MM:SS AM/PM date validation regular expression in javascript
Hope it helps!
Change of your regular expression would do it.
Use the following regular expression
^([1-9]|([012][0-9])|(3[01]))-([0]{0,1}[1-9]|1[012])-\d\d\d\d [012]{0,1}[0-9]:[0-5][0-9]:[0-5][0-9]$
It will validate the value 01-12-2011 19:59:59
<asp:RegularExpressionValidator ID="regDateFrom"
ValidationExpression="^([1-9]|([012][0-9])|(3[01]))-([0]{0,1}[1-9]|1[012])-\d\d\d\d [012]{0,1}[0-9]:[0-5][0-9]:[0-5][0-9]$"
ControlToValidate="txtDateFrom" ValidationGroup="Promotion"
runat="server"
ErrorMessage="Invalid Date">
</asp:RegularExpressionValidator>
I have a date picker where I am taking input from as user as MM/DD/YYYY. I want to validate it with a regular expression. Find Below code it might be help full.
expression = "^([0]{0,1}[1-9]|1[012])/([1-9]|([012][0-9])|(3[01]))/[0-9]{4}$";
I need to make a textbox that will select date range. It needs to display two calendars (Start Date and End Date) when it gets clicked. I am using Ajax control toolkit but it shows only one calendar and when I add two calendars with same TargetControlID, it still shows one calender.
<asp:TextBox ID="startDate" Text="Start Date" ReadOnly="False" EnableViewState="True" CssClass="calendar" runat="server">
</asp:TextBox>
cc1:CalendarExtender EnabledOnClient="True" DefaultView="Days" PopupButtonID="calenderopener"
ID="startDate_CalendarExtender" StartDate="Jan 15, 2014" CssClass="ajax__calendar ajax__calendar_container" EnableViewState="True"
runat="server" BehaviorID="calendar1" Enabled="True" TargetControlID="startDate" Format="MM/dd/yyyy">
</cc1:CalendarExtender>
I want to show two calendars when this textbox gets clicked.
Try this. This allows you to select multiple dates to single control(Textbox). If you want to use date range (only two days are selectable) then you shoud have to validate it in your code behind.
http://www.obout.com/calendar/tutorial_select_multipledates.aspx