验证两个日期字段,当1st必须在今天之前的7天内并且第二是今天或昨天 [英] Validating two date fields when 1st must be within 7days before today & 2nd is today or yesterday

查看:57
本文介绍了验证两个日期字段,当1st必须在今天之前的7天内并且第二是今天或昨天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



PlZz帮我任何一个。

我的问题是我想要两个日期归档为From和To。 来自我必须在今天前7天(如果今天是2015年6月15日它只接受2015年1月31日至2015年6月2日),至日期必须是今天或昨天(2015年5月5日或2015年6月6日)。

如果我在到日期选择昨天(2015年5月5日),从日期的最大值变为昨天(即范围是2015年1月31日至2015年5月5日)。

在这里,我使用Ajax Calendarextender和范围验证完成但问题来自于字段中显示的日期格式(From和To)必须在dd / MM / yyyy中。我在MM / dd / yyyy完成了。如果我将范围验证中的格式从MM / dd / yyyy更改为dd / MM / yyyy,则显示错误。我希望错误按摩也显示合适(例如从日期开始必须在今天之前的7天内,到目前为止必须是今天或昨天,你选择的日期为昨天,所以迄今为止不超过昨天)。 />
我不想改变页面文化。并且页面不能刷新每个选择。我想用任何方式(Ajax或JavaScript或JQuery或者无论如何)使用c#.net。

我的要求是日期格式必须是dd / MM / yyyy并且它们显示适当的错误。



。Aspx: -  
~~~~~~
<% @ Page 语言 = C# AutoEventWireup = true CodeFile < span class =code-keyword> = Default.aspx.cs < span class =code-attribute>继承 = _默认 %>

<%@ 注册 汇编 = AjaxControlToolkit 命名空间 = AjaxControlToolkit TagPrefix = ajx %>

< ! DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR /xhtml1/DTD/xhtml1-transitional.dtd\">

< ; html xmlns < span class =code-keyword> = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > Untitled Page < / title >
< script type = text / javascript >

< / script >

< / he广告 >
< 正文 >
< 表格 id = form1 runat = 服务器 >
< span class =code-keyword>< div >
< ajx:ToolkitScriptManager ID = 工具包criptManager1 runat = server >
< / ajx:ToolkitScriptManager >
< asp:标签 ID = Label1 < span class =code-attribute> runat = server 文字 = 来自 > < / asp:标签 >
< asp:TextBox ID = TextBox1 runat = server ReadOnly = true > < / asp :TextBox >
< asp:ImageButton ID = ImageButton1 runat = server ImageUrl = 01.png ImageAlign = 底部 / >
< asp:RangeValidator ID = RangeValidator1 ValidationGroup = datevalid runat = server ErrorMessage = 请选择在距离今天7天之前的范围内 类型 = 日期 ControlToValidate = TextBox1 显示 = 动态 > < / asp:RangeValidator >
< ajx:calendarextender ID = Calendarextender1 PopupButtonID = ImageButton1 TargetControlID = TextBox1 格式 = MM / dd / yyyy runat = 服务器 > < / ajx :calendarextender >
< asp:标签 ID = Label2 runat < span class =code-keyword> = server 文本 = > < / asp:Label >
< asp:TextBox ID = TextBox2 runat = 服务器 ReadOnly = true > < / asp:TextBox >
< asp:ImageButton ID = ImageButton2 runat = server ImageUrl = 01.png ImageAlign = 底部 < span class =code-keyword> / >
< asp:RangeValidator ID = RangeValidator2 ValidationGroup = datevalid runat = server 类型 = 日期 ErrorMessage = 日期必须是昨天或今天 ControlToValidate = TextBox2 显示 = 动态 > < / asp:RangeValidator >
< ajx:calendarextender ID = Calendarextender2 PopupButtonID = ImageButton2 TargetControlID < span class =code-keyword> = TextBox2 格式 = MM / dd / yyyy runat = 服务器 > < / ajx:calendarextender >
< br / >
< asp:CompareValidator ID = CompareValidator1 runat = 服务器 类型 = 日期 ErrorMessage = 日期必须低于昨天 ControlTo比较 = TextBox2 ControlToValidate = TextBox1 运算符 = LessThanEqual > < / asp:CompareValidator >
< / div >
< / form >
< < span class =code-leadattribute> / body >
< / html >


.CS: -
~~~~~
使用System;
使用System.Configuration;
使用System.Data;
使用System.Linq;
使用System.Web;使用System.Web.Security
;
使用System.Web.UI;
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq;

public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{

if(IsPostBack)
{

}
}

protected void datevalid(object sender,EventArgs e)
{


}
protected void Page_PreRender(object sender,EventArgs e)
{
DateTime d1 = DateTime.Today;
DateTime d2 = DateTime.Today.Date.AddDays(-6);
DateTime d3 = DateTime.Today.Date.AddDays(-1);

RangeValidator1.MaximumValue = d1.ToString(MM-dd-yyyy);
RangeValidator1.MinimumValue = d2.ToString(MM-dd-yyyy);
RangeValidator2.MaximumValue = d1.ToString(MM-dd-yyyy);
RangeValidator2.MinimumValue = d3.ToString(MM-dd-yyyy);
}
}

解决方案

我找到了答案。



http://forums.asp.net/t/2033949.aspx?asp+net+Validating+two+date+fields+when+1st+must+be+within+7days+before+today+第二是+今天+或+昨天

Hi,
PlZz Help me any one.
Here my problem is I want two date filed as "From" and "To". "From" must me with is 7 days before today (if today is 6th feb 2015 it accepts only 31st jan 2015 to 6th feb 2015) and "To" date must be today or yesterday (5th feb 2015 or 6th feb 2015).
If I select yesterday(5th feb 2015) on "To" date the maximam value of "From" date becomes yesterday,( i.e the range is 31st jan 2015 to 5th feb 2015).
Here I done by using Ajax Calendarextender and range validate but problem come here is date formate shown in fields ("From" and "To") must be in dd/MM/yyyy. I done in MM/dd/yyyy. If I change the formate in range validate from MM/dd/yyyy to dd/MM/yyyy it is showing error. I want error massages also showing appropriate (Like "From date must be within 7 days before today", "To date must be Today or yesterday", "You selected To date as yesterday so To date not more than yesterday").
I don’t want to change culture of page. And Page must not refresh every selection. I want it any way (Ajax or JavaScript or JQuery or anyway) using c#.net.
My requirement is date format must be dd/MM/yyyy and they show appropriate errors.

.Aspx:-
~~~~~~
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">

</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <ajx:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </ajx:ToolkitScriptManager>
        <asp:Label ID="Label1" runat="server" Text="From"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" ReadOnly="true"></asp:TextBox>
        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="01.png" ImageAlign="Bottom"/>
        <asp:RangeValidator ID="RangeValidator1" ValidationGroup="datevalid" runat="server" ErrorMessage="Please select with in range before 7 days from today" Type="Date" ControlToValidate="TextBox1" Display="Dynamic"></asp:RangeValidator>
    <ajx:calendarextender ID="Calendarextender1" PopupButtonID="ImageButton1" TargetControlID="TextBox1" Format="MM/dd/yyyy" runat="server"></ajx:calendarextender>
    <asp:Label ID="Label2" runat="server" Text="To"></asp:Label>
    <asp:TextBox ID="TextBox2" runat="server" ReadOnly="true"></asp:TextBox>
        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="01.png" ImageAlign="Bottom"/>
        <asp:RangeValidator ID="RangeValidator2" ValidationGroup="datevalid" runat="server" Type="Date" ErrorMessage="date must be yesterday or today" ControlToValidate="TextBox2" Display="Dynamic"></asp:RangeValidator>
        <ajx:calendarextender ID="Calendarextender2" PopupButtonID="ImageButton2" TargetControlID="TextBox2" Format="MM/dd/yyyy" runat="server"></ajx:calendarextender>
        <br/>
        <asp:CompareValidator ID="CompareValidator1" runat="server" Type="Date" ErrorMessage="From Date mustbe lessthan yesterday"  ControlToCompare="TextBox2" ControlToValidate="TextBox1" Operator="LessThanEqual"></asp:CompareValidator>
    </div>
    </form>
</body>
</html>


.CS:-
~~~~~
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
        {

        }
    }

    protected void datevalid(object sender, EventArgs e)
    {


    }
    protected void Page_PreRender(object sender, EventArgs e)
    {
        DateTime d1 = DateTime.Today;
        DateTime d2 = DateTime.Today.Date.AddDays(-6);
        DateTime d3 = DateTime.Today.Date.AddDays(-1);

        RangeValidator1.MaximumValue = d1.ToString("MM-dd-yyyy");
        RangeValidator1.MinimumValue = d2.ToString("MM-dd-yyyy");
        RangeValidator2.MaximumValue = d1.ToString("MM-dd-yyyy");
        RangeValidator2.MinimumValue = d3.ToString("MM-dd-yyyy");
    }
}

解决方案

I found answer to this.

http://forums.asp.net/t/2033949.aspx?asp+net+Validating+two+date+fields+when+1st+must+be+within+7days+before+today+2nd+is+today+or+yesterday


这篇关于验证两个日期字段,当1st必须在今天之前的7天内并且第二是今天或昨天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆