简体   繁体   中英

Allowing only past date and today's date in VB.net

I am using VB.net as well as Jquery Datepicker for getting dates.

In my VB.net code

<tr>
    <td>
        DateOfReceiving:
    </td>
    <td colspan="3">
        <asp:TextBox ID="DateOfReceivingTextBox" runat="server"
                     CssClass="pastdatepicker" 
                     Text="DateOfReceiving" />
    </td>
</tr>

I want to allow enter user only todays date or past date with format dd/mm/yyyy . I want vb.net custom validation for that.

Please help to write vb.net regular expression.

You can do this with a RangeValidator and a bit of VB.NET code:

<tr>    
    <td>DateOfReceiving:</td>
    <td colspan="3">
        <asp:TextBox ID="DateOfReceivingTextBox" runat="server"  
                     CssClass="pastdatepicker" 
                     Text="DateOfReceiving" />

        <asp:rangevalidator runat="server" ID="DateRangeValidator"
                            ControlToValidate="DateofReceivingTextBox"
                            ErrorMessage="Date must be in the past"  
                            Display="Dynamic" Type="Date"  />

    </td>
</tr>

Set the range for the validator in your code-behind, most likely in your Page_Load:

DateRangeValidator.MinimumValue = Date.MinValue.ToString("yyyy/MM/dd")
DateRangeValidator.MaximumValue = Date.Today.ToString("yyyy/MM/dd")

You aren't going to want to use a regular expression for this.

If you want to do this server-side, you should look into creating validation controls. I've never done this in VB.NET (simply because I've never used VB.NET - I'm more comfortable with C#), but this page appears to be a good starting point. You are going to want to inherit from and override methods in the BaseValidator class.

I don't usually write client-side code unless I have to, but it looks like jQuery supports form validation as well. However, as brad.huffman pointed out in the comments, you probably want to avoid doing date/time validation on the client side as you can't control your user's date/time settings - they could be incorrect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM