简体   繁体   English

阻止用户在Calendar ASP中阻止的文本框中输入日期

[英]Prevent users from entering date in textbox which is blocked in Calendar ASP

I have a textbox which has a calendar icon at its right side. 我有一个文本框,其右侧有一个日历图标。 When I click icon, the calendar appears which allows user to select a particular date. 当我单击图标时,将出现日历,允许用户选择特定日期。 In calendar the user cannot select the previous date and also the calendar is blocked for 2 years from the current date. 在日历中,用户无法选择上一个日期,并且日历从当前日期开始被阻止2年。

Now I have to validate the textbox. 现在,我必须验证文本框。

  1. If user manually enters any past date it gives an error " Invalid Date" - which is correct 如果用户手动输入任何过去的日期,则会出现错误“无效的日期”-这是正确的
  2. But if the user enters future date ie, a date after 2 years from current date, the system accepts which is incorrect. 但是,如果用户输入将来的日期,即距当前日期2年后的日期,则系统接受不正确的日期。

So I want to validate textbox so that user gets an error "Invalid Date" if he enters a date now available in calendar and I don't want Read Only textbox. 因此,我想验证文本框,以便用户输入日历中现在可用的日期并且我不希望使用“只读”文本框时,会收到“无效日期”错误。

Code

<script type="text/javascript">

    window.onload = function () {
        var dateToday = new Date();
        $("input.dtpRelease").datepicker({

            showOn: 'button',
            changeYear: true,
            changeMonth:true,
            minDate: dateToday,
            maxDate: "2y",
            buttonImageOnly: true,
            dateFormat: "mm/dd/yy",
            buttonImage: '/Images/icon_calendar_24.png',

        });
   }

<asp:TextBox ID="dtpStartDate" runat="server" Style="width: 80px; margin-top: 5px;" CssClass="form-control dp2 dtpRelease pull-left" MaxLength="100" placeholder="Start Date" Text='<%# MSEncoder.Encoder.HtmlEncode(Convert.ToString( Eval("FulfillmentStartDate"))) %>' onblur="ValidateDate(this)"></asp:TextBox> 
                           <br/><br/><asp:RegularExpressionValidator ID="regExSDate" runat="server" ControlToValidate="dtpStartDate" ForeColor="Red" Display="Dynamic" ValidationExpression="^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$" ErrorMessage="Please enter date in MM/DD/YYYY Format"  ValidationGroup="vg" ></asp:RegularExpressionValidator>
                                     <asp:CompareValidator ID="cvStartDate" runat="server" ControlToValidate="dtpStartDate" ErrorMessage="Invalid Start Date" ForeColor="Red" Operator="GreaterThanEqual"  Display="Dynamic" ValidationGroup="FirstPreview" CssClass="validatorMsg" ValueToCompare="<%# DateTime.Today.ToShortDateString() %>" SetFocusOnError="True"  Type="Date" ></asp:CompareValidator> 

why dot you just disable the textbox, and let the user enter the date through the datepicker only. 为什么只点关闭文本框,让用户仅通过日期选择器输入日期。 it will be exactly like this demo , but you can trigger the calendar from an image beside the textbox 就像这个演示一样 ,但是您可以从文本框旁边的图像触发日历

you can use the below function to check wither the date in textbox is within range or not 您可以使用以下功能检查文本框中的日期是否在范围内

<script>
   function checkDate(d)
   {
       var maxAcceptedDate = new Date()
       maxAcceptedDate.setFullYear(maxAcceptedDate.getFullYear()+2)

       return d <= maxAcceptedDate
   }
</script>

if you are going to call this js function from your custom validator you need to change the method to make sure it wont take any arguments 如果要从自定义验证器调用此js函数,则需要更改方法以确保它不接受任何参数

<script>
   function checkDate()
   {
      //var d = [get text from your textbox as date]
       var maxAcceptedDate = new Date()
       maxAcceptedDate.setFullYear(maxAcceptedDate.getFullYear()+2)

       return d<=maxAcceptedDate
   }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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