如何阻止用户将时间字段留空?

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

我遇到的问题是,当用户跳过循环时间时,即使有所需的字段验证器,它仍然允许提交并使页面崩溃。我尝试设置一个初始值,但没有用。

<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

写这篇文章是为了解决这个问题,现在表单可以正常工作了。

 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

您可以在回发时尝试服务器端验证:

Dim Cycle_Time_Sec As Integer = 0
Dim My_Time As String = ""

//Validation here
If txtCycle_Time.Text is null Then return "Please type time value!"

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