C# Asp.net 比较验证器以验证日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9372901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:04:53  来源:igfitidea点击:

Asp.net compare validator to validate date

c#asp.netvalidationdatecomparevalidator

提问by Deeptechtons

As you all know Compare validators can be used to validate dates and check based on operator type (<, <= , >= etc). I have set the cultureinvariantvalues="true"property to validate two textbox controls that hold dates. I have to constrain them such that the start date must be earlier than the finish date. The validation seems to fail when I type a descriptive date like below:

众所周知,比较验证器可用于验证日期并根据运算符类型(<、<=、>= 等)进行检查。我已将该cultureinvariantvalues="true"属性设置为验证两个保存日期的文本框控件。我必须限制它们,使开始日期必须早于完成日期。当我输入如下描述性日期时,验证似乎失败:

StartDate: Tuesday, 21 February 2012

FinishDate: Wednesday, 22 February 2012

Even though 22nd is larger than 21st the validation fails. The markup I used is below. If for any reason you need format info, here it is dddd, dd MMMM yyyy

即使第 22 次大于第 21 次,验证也会失败。我使用的标记如下。如果出于任何原因您需要格式信息,这里是dddd, dd MMMM yyyy

<asp:CompareValidator id="cvtxtStartDate" runat="server" 
       controltocompare="txtFinishDate" 
       cultureinvariantvalues="true" 
       display="Dynamic" 
       enableclientscript="true" 
       controltovalidate="txtStartDate" 
       errormessage="Start date must be earlier than finish date" 
       type="Date" 
       setfocusonerror="true" 
       operator="LessThanEqual" 
       text="Start date must be earlier than finish date">

采纳答案by Ebad Masood

Try this approach, First Enter the Start Date and Check the Compare Validator with the End Date textbox:

尝试这种方法,首先输入开始日期并检查比较验证器与结束日期文本框:

<asp:CompareValidator id="cvtxtStartDate" runat="server" 
     ControlToCompare="txtStartDate" cultureinvariantvalues="true" 
     display="Dynamic" enableclientscript="true"  
     ControlToValidate="txtFinishDate" 
     ErrorMessage="Start date must be earlier than finish date"
     type="Date" setfocusonerror="true" Operator="GreaterThanEqual" 
     text="Start date must be earlier than finish date"></asp:CompareValidator>

回答by Neeraj Gulia

Try custom Validator and at the code behind at onservervalidate event convert the text to DateTime and then do the comparision.

尝试自定义验证器,并在 onservervalidate 事件后面的代码中将文本转换为 DateTime,然后进行比较。

protected void DateTimeComparision_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = Convert.ToDateTime(txtStartDate.Text) < Convert.ToDateTime(txtFinishDate.Text);
    }

回答by Abhishek kumar

Compare validator has the type=date.But that date type is constrained to accept only particular format of date i.e ToShortDateString(). If the date format of the two textboxes to be compared is in some other format like ToLongDateString() or some format specified by ToString("dd MMMM,yyyy") the comparision does not work. CustomValidator isonly option. If you want to use compare validator only then

比较验证器具有类型=日期。但该日期类型被限制为仅接受特定格式的日期,即 ToShortDateString()。如果要比较的两个文本框的日期格式是其他格式,如 ToLongDateString() 或 ToString("dd MMMM,yyyy") 指定的某种格式,则比较不起作用。CustomValidator 是唯一的选项。如果您只想使用比较验证器

textstartdate.text=Calendar1.SelectedDate.ToShortDateString();
textfinishdate=Calendar2.SelectedDate.ToShortDateString();
<asp:CompareValidator ID="CompareValidator4" runat="server" 
                    ControlToCompare="textstartdate" ControlToValidate="textfinishdate" 
                    CultureInvariantValues="True" 
                    ErrorMessage="Date should be greater than booking date." 
                    Operator="GreaterThanEqual" SetFocusOnError="True" Type="Date"></asp:CompareValidator>

回答by Deenathaiyalan Shanmugam

function FromAndToDateValidate() {
try {
    var StartDate = new Date();
    StartDate = $("#dtpFromDate").val();

    var EndDate = new Date();
    EndDate = $("#dtpToDate").val();
    args.IsValid = (StartDate <= EndDate);
}
catch (ex) {
    alert(ex);
}
}