I am trying to allow users to only access the dates with in the current month. I have tested by trying a future date with in the same year (2/26/2013) and have success but when I try 1/26/2012 it fails to notify the user that the date range is not correct.
Here is my code.
That sounded promising but did not work. I know I am just working around a problem instead of fixing it but what I did was I ended solving my issue in the C# section of this project.
bool ValidateDate()
{
DateTime Min = DateTime.Now;
DateTime Max = DateTime.Now;
Min = new DateTime(Min.Year, Min.Month, 1);
Max = new DateTime(Max.Year, Max.Month, DateTime.DaysInMonth(Max.Year, Max.Month));
DateTime UserInput = new DateTime();
try
{
UserInput = DateTime.Parse(txtDate.Text);
}
catch
{
return false;
}
if (UserInput.Date <= Max.Date & UserInput.Date >= Min.Date)
{
return true;
}
else
return false;
}
obviously this is called before the client can save a record into the database. If its true its good to go, if not, whoops no can do buddy.
Marv102
Member
4 Points
12 Posts
Validating date range in text box works for future dates but not past dates
Jan 26, 2013 05:29 PM|LINK
I am trying to allow users to only access the dates with in the current month. I have tested by trying a future date with in the same year (2/26/2013) and have success but when I try 1/26/2012 it fails to notify the user that the date range is not correct. Here is my code.
C#
protected void Page_Load(object sender, EventArgs e) { if (DataStore.CurrentUser.CitizenID > 0) { lblWelcome.Text = "Welcome " + DataStore.CurrentUser.UserName + "-- Plate # "+DataStore.CurrentUser.Lisence + " "+DataStore.CurrentUser.Make + " " + DataStore.CurrentUser.Model + " " +DataStore.CurrentUser.Year.ToString(); int Num = Queries.checkCallerForLimit(DataStore.CurrentUser.Lisence); if (!Page.IsPostBack) { cmbNumDays.Items.Clear(); DisplayAvailableRequestsRemaining(Num); } Queries.getStreets(); Queries.getReasons(); SnowDates = Queries.getSnowDates(); Blankets = Queries.getBlankets(); FillFirstComboBoxes(); FillCurrentMonthRequests(); FigureOutAddressRange(); SetDateRangeValidator(); } else { Response.Redirect("~/RestrictedAccess.aspx"); } }void SetDateRangeValidator() { DateTime Min = DateTime.Now; DateTime Max = DateTime.Now; Min = new DateTime(Min.Year, Min.Month, 1); Max = new DateTime(Max.Year, Max.Month, DateTime.DaysInMonth(Max.Year, Max.Month)); DateRangeValidator.MinimumValue = Min.ToShortDateString(); DateRangeValidator.MaximumValue = Max.ToShortDateString(); }asp
<td class="auto-style5"> <asp:TextBox ID="txtDate" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="txtDate" ErrorMessage="Date required" ForeColor="Red">*</asp:RequiredFieldValidator> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtDate" ErrorMessage="Date format should be ##/##/####" ForeColor="Red" Operator="DataTypeCheck" Type="Date">!</asp:CompareValidator> <asp:RangeValidator ID="DateRangeValidator" runat="server" ControlToValidate="txtDate" ErrorMessage="Date must be in current month" MaximumValue="12/31/2013" MinimumValue="1/1/2013">!</asp:RangeValidator> </td>MetalAsp.Net
All-Star
112051 Points
18235 Posts
Moderator
Re: Validating date range in text box works for future dates but not past dates
Jan 26, 2013 06:49 PM|LINK
Marv102
Member
4 Points
12 Posts
Re: Validating date range in text box works for future dates but not past dates
Jan 26, 2013 07:41 PM|LINK
That sounded promising but did not work. I know I am just working around a problem instead of fixing it but what I did was I ended solving my issue in the C# section of this project.
bool ValidateDate() { DateTime Min = DateTime.Now; DateTime Max = DateTime.Now; Min = new DateTime(Min.Year, Min.Month, 1); Max = new DateTime(Max.Year, Max.Month, DateTime.DaysInMonth(Max.Year, Max.Month)); DateTime UserInput = new DateTime(); try { UserInput = DateTime.Parse(txtDate.Text); } catch { return false; } if (UserInput.Date <= Max.Date & UserInput.Date >= Min.Date) { return true; } else return false; }obviously this is called before the client can save a record into the database. If its true its good to go, if not, whoops no can do buddy.