Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, I'm not a C# or ASP.NET master but assuming that your template code looks something like following then the first step is going to be editing the StartTimes, EndTimes and Date functions so that they return only times and dates that are in the valid range AND after the current time like the following.</p> <pre><code>public class TimeHelper { public DateTime Start { get; private set; } public DateTime End { get; private set; } public static List&lt;TimeSpan&gt; TimeSpansInRange(TimeSpan start, TimeSpan end, TimeSpan interval) { List&lt;TimeSpan&gt; timeSpans = new List&lt;TimeSpan&gt;(); TimeSpan now = DateTime.Now.TimeOfDay; while (start.Add(interval) &lt;= end) { if(start.Add(interval) &gt; now){ timeSpans.Add(start); } start = start.Add(interval); } return timeSpans; } public static List&lt;TimeSpan&gt; PossibleTimeSpansInDay() { return TimeSpansInRange(new TimeSpan(8, 0, 0), new TimeSpan(22, 30, 0), new TimeSpan(0, 30, 0)); } } </code></pre> <p>That should take care of the first part of your question.</p> <pre><code>&lt;label for="Date"&gt;Date&lt;/label&gt; &lt;%= Html.TextBox("Date", Model.Request.Date, new { @class="required" })%&gt; &lt;label for="Start"&gt;StartTime&lt;/label&gt; &lt;%=Html.DropDownList("Start",Model.Request.StartTimes, { new{@class="required"})%&gt; &lt;%= Html.ValidationMessage("Start", "")%&gt; &lt;label for="End"&gt;EndTime&lt;/label&gt; &lt;%=Html.DropDownList("End",Model.Request.EndTimes, { new{@class="required"})%&gt; &lt;%= Html.ValidationMessage("End", "")%&gt; </code></pre> <p>For the second part of the question (disallowing invalid end times) we're going to add some javascript and markup. Now I don't know, but I'm going to assume that you have a submit input item to submit the form with the time date. Like follows:</p> <pre><code>&lt;input type="submit" value="Submit" /&gt; </code></pre> <p>We're going to replace that input item with a button and some javascript code.</p> <p>HTML</p> <pre><code>&lt;button onclick="checkTimes" /&gt; </code></pre> <p>Javascript</p> <pre><code>function checkTimes(){ start = setTime(new Date(), $('#start_id')); end = setTime(new Date(), $('#end_id')); if(end &gt; start){ $('form_id').submit(); } else { alert("End time must be greater then start time"); } } function setTime(time, field){ re = /^(\d{1,2}):(\d{2})(:00)$/; if(regs = field.value.match(re)) { time.setHours(regs[1], regs[2], 0, 0); } return time; } </code></pre> <p>Now this code make some assumptions, namely that the start and end select fields and the form have ids named start_id, end_id and form_id respectively. That part of the code in the checkTimes function should be changed to whatever their ids actually are. I also assume that the time is in 00:00:00 format, if that's not the case just change the value of re in the setTime function as appropriate. I hope this is helpful to you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload