急问是不是要用到customvalidator?

解决方案 »

  1.   

    主要是你问的不清楚:
    如果是web form:
    1.写客户端js脚本进行验证;
    2.写服务器端程序在页面提交的时候验证;
    3.也可以自定义CustomValidator进行验证,验证事件的代码与2差不多
      

  2.   

    我给相关代码,和报错,大家看看Dim i as integer
    Dim Firstdate as Date
    i=weekday(today(),7)
    FirstDate=today.AddDays(-i)
    ..............<TD height="42" width=263><asp:textbox id="txtTime" runat="server" Width="250px" MaxLength="100" Columns="50" Height="30px"></asp:textbox></TD>
    ..............
    <asp:RangeValidator ID="rangeValDate" Type="Date" display="Dynamic" ControlToValidate="txttime" ErrorMessage="错误的日期!" MaximumValue=datetime.today
    MinimumValue=Firstdate  Runat="server"/>报错信息
    The value 'datetime.today' of the MaximumValue property of 'rangeValDate' cannot be converted to type 'Date'. 
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: The value 'datetime.today' of the MaximumValue property of 'rangeValDate' cannot be converted to type 'Date'.Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
      

  3.   

    RangeValidator的MaximumValue和MinimumValue是字符串类型,给它赋值日期类型当然报Exception。
    另用RangeValidator验证日期时间类型要注意:如果在未以编程方式设置应用程序区域性的情况下为 BaseCompareValidator.Type 属性指定 ValidationDataType.Date,则应将非特定区域性的格式(如 YYYY/MM/DD)用于 MaximumValue 和 MinimumValue 属性。否则,可能无法正确解释日期。
      

  4.   

    那请问这段程序应该怎么写?验证控件里面不是有type=date吗?
      

  5.   

    把你的Today和FirstDate转换为字符串赋值呀(注意转换的格式)
      

  6.   

    MaximumValue=DateTime.Today.ToString("YYYY/MM/DD");
    MinimumValue=Firstdate.ToString("YYYY/MM/DD");
      

  7.   

    这是C#的服务器端验证:     
     void ServerValidation (object source, ServerValidateEventArgs arguments)
          {
             
                DateTime dt = DateTime.Parse(arguments.Value);
       DateTime no = DateTime.Now;
       DateTime ol = no.AddDays(-7);
       arguments.IsValid = ((dt>ol)&&(dt<no));      }
    验证控件:
          <asp:CustomValidator id="CustomValidator1"
               ControlToValidate="Text1"
               OnServerValidate="ServerValidation"
               Display="Static"
               ErrorMessage="Not an even number!"
               ForeColor="green"
               Font-Name="verdana" 
               Font-Size="10pt"
               runat="server"/>
      

  8.   

    补充:把"YYYY/MM/DD"改为"yyyy/MM/dd"
      

  9.   

    写了个示例文件,自己保存为aspx文件后运行一下看看:<script language="C#" runat="server">
    const string CookieName="WebdiyersCookie";
    void Page_Load(object src,EventArgs e){
    if(!Page.IsPostBack)
    minDTbox.Text="2003-9-1";
    val1.MaximumValue=DateTime.Today.ToString("yyyy/MM/dd");
    val1.MinimumValue=(DateTime.Parse(minDTbox.Text)).ToString("yyyy/MM/dd");
    }</script>
    <HTML><HEAD>
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="Webdiyer">
    </HEAD><BODY>
    <form runat="server">
    <asp:Label runat="server" id="lbl"/><p>
    最小日期:<asp:TextBox runat="server" id="minDTbox" Value="2003-9-1"/>
    <asp:Button runat="server" Text="设置最小日期" id="btn1" CausesValidation=false/>
    <p>
    <asp:TextBox runat="server" id="dtbox"/>
    <asp:RequiredFieldValidator runat="server" ErrorMessage="必须输入一个日期值" ControlToValidate="dtbox" Display="dynamic"/>
    <asp:CompareValidator runat="server" ErrorMessage="日期格式错误" Operator="DataTypeCheck" Type="Date" ControlToValidate="dtbox" Display="dynamic"/>
    <asp:RangeValidator runat="server" id="val1" Type="Date" ErrorMessage="日期太大或太小" ControlToValidate="dtbox" Display="dynamic"/>
    <asp:Button runat="server" Text="提交"/>
    </form></BODY></HTML>
      

  10.   

    Parser Error Message: The server tag is not well formed.Source Error: 
    Line 256: <asp:button id="butAdd" onclick="Add_Click" runat="server" Width="120px" Type="submit" text="   添加   "></asp:button><BR>
    Line 257: <BR>
    Line 258: <asp:RangeValidator ID="rangeValDate" Type="Date" display="Dynamic" ControlToValidate="txttime" ErrorMessage="错误的日期!" MaximumValue=DateTime.Today.ToString("yyyy/MM/dd")
    Line 259: MinimumValue="2002-10-1" Runat="server"/>
    Line 260:
     Source File: c:\inetpub\wwwroot\WebApplication3\log.aspx    Line: 258
      

  11.   

    MaximumValue=DateTime.Today.ToString("yyyy/MM/dd")
    这句要写在后台代码中:
    rangeValDate.MaximumValue=DateTime.Today.ToString("yyyy/MM/dd")
      

  12.   

    判断一下就可以了啊string x = string.Format("{0:yyyy/MM/dd}",DateTime.Now);
    string b = string.Format("{0:yyyy/MM/dd}",DateTime.Now.AddDays(1));