[SOLVED] How do I stop a user from leaving time field empty?

Issue

The problem I’m having is when a user skips over cycle time, even with the required field validator in place it allows to to still submit and crash the page. I tried setting an initial value but that didn’t work.

<div class="col-lg-4" style="text-align: left;">
    <label for="txtCycle_Time">Cycle Time (format mm:ss) :</label>
    <asp:TextBox ID="txtCycle_Time" MaxLength="5" CssClass="form-control" runat="server"></asp:TextBox>
    <ajaxToolkit:MaskedEditExtender ID="txtCycle_Time_MaskedEditExtender" AutoComplete="true" MaskType="Time" AcceptAMPM="false" runat="server" MessageValidatorTip="true" TargetControlID="txtCycle_Time" Mask="99:99" ClearMaskOnLostFocus="false" />
    <asp:RequiredFieldValidator ID="txtCycle_Time_RequiredFieldValidator" runat="server" InitialValue="00:00" Text="Please Enter Cycle Time" ControlToValidate="txtCycle_Time" ValidationGroup="Input" Font-Italic="True" ForeColor="Red" />
</div>
Dim Cycle_Time_Sec As Integer = 0
Dim My_Time As String = ""
My_Time = txtCycle_Time.Text
Dim Min As String = My_Time.Substring(0, 2)
Dim Sec As String = My_Time.Substring(3, 2)
Cycle_Time_Sec = Min * 60 + Sec

Solution

Wrote this to fix the issue, now form works as intended.

 If (txtCycle_Time.Text.Trim = "__:__" Or txtCycle_Time.Text.Trim = "__:__ AM" 
      Or txtCycle_Time.Text.Trim = "__:__ PM") Then ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Cycle_Time_Error", "Cycle_Time_Error();", True)
            Exit Sub
        End If

Answered By – Mr.Skellington

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *