Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正在验证当前月份内的日期_Javascript_Vb.net_Validation - Fatal编程技术网

Javascript 正在验证当前月份内的日期

Javascript 正在验证当前月份内的日期,javascript,vb.net,validation,Javascript,Vb.net,Validation,我正在处理发票的日期字段。为发票日期选择的日期不能是当月以外的任何一天 这是我的密码: at Page_Load: Dim firstOfTheMonthDate As DateTime = FirstDayOfMonthFromDateTime(DateTime.Now) Me.compareValidatorDate.ValueToCompare = firstOfTheMonthDate.ToString("d") 我的私人职能 Private Func

我正在处理发票的日期字段。为发票日期选择的日期不能是当月以外的任何一天

这是我的密码:

at Page_Load:
 Dim firstOfTheMonthDate As DateTime = FirstDayOfMonthFromDateTime(DateTime.Now)
                Me.compareValidatorDate.ValueToCompare = firstOfTheMonthDate.ToString("d")
我的私人职能

Private Function FirstDayOfMonthFromDateTime(dateTime As DateTime) As DateTime
        Return New DateTime(dateTime.Year, dateTime.Month, 1)
    End Function
客户端:

<asp:Label ID="lblInvDate" runat="server" AssociatedControlID="txtInvDate">Invoice Date:</asp:Label>
                    <asp:TextBox runat="server" ID="txtInvDate" MaxLength="20" CssClass="L5 DateCal" />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtInvDate" 
                    Text="The date field is required!" runat="server" />
                    <asp:CompareValidator ID="compareValidatorDate" ControlToValidate="txtInvDate" 
                    Type="Date" Operator="LessThan" ErrorMessage="Date must be from this month!"
                    Display="Dynamic" runat="server" />
<asp:Label ID="lblInvDate" runat="server" AssociatedControlID="txtInvDate">Invoice Date:</asp:Label>
<asp:TextBox runat="server" ID="txtInvDate" MaxLength="20" CssClass="L5 DateCal" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtInvDate" 
    Text="The date field is required!" runat="server" />
<asp:RangeValidator id="rangeValidatorDate" runat="server"
    ControlToValidate="txtInvDate" Type="Date" Display="Dynamic" />
发票日期:
我遇到的问题是没有进行验证,或者至少,如果输入的日期不是空白或null,则保存发票日期。如何改进代码


感谢Karl Anderson对代码的帮助。

我很抱歉之前没有注意到这一点,但我之前给出了错误的逻辑

更好的解决方案是使用
RangeValidator
,如下所示:

代码隐藏(页面加载):

代码隐藏(实用程序功能):

客户端:

<asp:Label ID="lblInvDate" runat="server" AssociatedControlID="txtInvDate">Invoice Date:</asp:Label>
                    <asp:TextBox runat="server" ID="txtInvDate" MaxLength="20" CssClass="L5 DateCal" />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtInvDate" 
                    Text="The date field is required!" runat="server" />
                    <asp:CompareValidator ID="compareValidatorDate" ControlToValidate="txtInvDate" 
                    Type="Date" Operator="LessThan" ErrorMessage="Date must be from this month!"
                    Display="Dynamic" runat="server" />
<asp:Label ID="lblInvDate" runat="server" AssociatedControlID="txtInvDate">Invoice Date:</asp:Label>
<asp:TextBox runat="server" ID="txtInvDate" MaxLength="20" CssClass="L5 DateCal" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtInvDate" 
    Text="The date field is required!" runat="server" />
<asp:RangeValidator id="rangeValidatorDate" runat="server"
    ControlToValidate="txtInvDate" Type="Date" Display="Dynamic" />
发票日期:

您的验证表明,在txtInvDate中输入的值必须小于任意月份的第一天。这似乎不是您所说的验证行为,您希望它大于或等于本月的第一天,小于或等于本月的最后一天。我对你的要求的理解正确吗?@Adrian-谢谢你注意到这一点,我的想法是直截了当的,但写的逻辑与你说的完全相反。是的,Adrian,绝对正确。因为最初的要求是日期“在当前月内”可能
rangeValidatorDate.MaximumValue
应该是
new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month))。这是一口,但它应该给出本月的最后一天。@Adrian-哇,我今天不吃了。很好看。最新答案。谢谢阿德里安和卡尔。我只是有足够的代表给道具,所以我会的。我真的很感谢您的帮助,并且我能够解释代码在所需断点之间的每一步实际执行的操作。调试进展顺利。