如何将Calender添加到文本框以及如何验证StartDate应该比EndDate更少 [英] How to add Calender to a Textbox and how to validate StartDate should be lessthan EndDate

查看:88
本文介绍了如何将Calender添加到文本框以及如何验证StartDate应该比EndDate更少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我需要为两个文本框添加日历

1.开始日期

2.结束日期

并验证StartDate应该比EndDate少。



我在谷歌搜索我发现了代码,但他们都使用HTML控件但在这里我使用的是Asp.Net控件。稍后我需要在Codebehind中访问那些StartDate和EndDate。



 <   p  >  
< asp:标签 ID < span class =code-keyword> = lblcourses runat = server 文字 = 课程名称 > < / asp:Label >
< asp:DropDownList ID = drpcourses runat = server >
< asp:ListItem 文字 = - 选择一个课程 - = 0 > < / asp:ListItem >
< asp:ListItem 文本 = 基本Unix = 1 > < / asp:ListItem >
< asp:ListItem 文本 = 基本Oracle = 2 >
< / asp:ListItem >
< asp:ListItem 文本 = PL / SQL = 3 > < / asp:ListItem >
< < span class =code-leadattribute> asp:ListItem 文本 = Oracle 11g DBA < span class =code-attribute>值 = 4 > < / asp:ListItem >
< asp:ListItem < span class =code-attribute> 文本 = Oracle 11g DBA F / T = 5 > < / asp:ListItem >
< asp:ListItem 文本 = Oracle 11g RAC F / T = 6 > < / asp:ListItem >
< asp:ListItem 文本 = Linux Admin = 7 > < / asp:ListItem >
< asp:ListItem 文字 = Unix管理 = 8 > < / asp:ListItem >
< ; asp:ListItem 文字 = Advance Shell = 9 > < / asp:ListItem >
< asp :ListItem 文本 = Hadoop管理 = 10 > < / asp:ListItem >
< span class =code-keyword>< / asp:DropDownList >
< / p >

< p >
< asp:Label ID = lblbatchname runat = 服务器 正文 = 批次名称 > < / asp:Label >
< asp:TextBox ID = txtbatchname runat = server ReadOnly = true > < / asp:TextBox >
< / p >

< p >
< asp:标签 ID = lblstartdate runat = server 文本 = 开始日期 > < / asp:标签 >
< asp:TextBox ID = txtstartdate runat = 服务器 > < / asp:TextBox >
< / p >

< < span class =code-leadattribute> p >
< asp:标签 ID = lblenddate runat = server 文字 = 结束日期 < span class =code-keyword>> < / asp:Label < span class =code-keyword>>
< asp:TextBox ID = txtenddate runat = 服务器 > < / asp:TextBox >
< / p >

< p >
< asp:按钮 ID = btnsave runat = server 文字 = 添加 OnClientClick = javascript:return ValidateDropDown(); / >
< / p >

解决方案

解决方案



请不要使用Plain TextBoxes for 日期操作。



您可以使用 jQuery DatePicker ,这很简单易用。

演示



[演示] JavaScript中的日期比较 [ ^ ]


如果你想使用javascript

试试



 function fnGo(){
lblMsg.innerHTML = ;
var Mon = new Date();
var CurMonth = Mon.getMonth()+ 1 ;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+ 1 ;
var yyyy = today.getFullYear();
if (dd < 10 ){dd = ' 0' + dd} if (mm < 10 ){mm = ' 0' + mm} var today = mm + ' /' + dd + ' /' + yyyy;

var fromdt = document.getElementById( txtstartdate)。 value ;
var todt = document.getElementById( txtenddate)。 value ;
var dtexp = fromdt.split( - )[ 1 ] + ' /' + fromdt.split( - )[ 0 ] + / + fromdt.split( - )[ 2 ];
var Mon = todt .split( - )[ 1 ] + ' /' + todt .split( - )[ 0 ] + / + fodt.split( - )[ 2 ];
if (Date.parse(dtexp)> Date.parse(today)){
lblMsg.innerText = 起始日期应小于或等于当前日期;
document.getElementById( txtstartdate)。focus();
return false ;
}
if (Date.parse(Mon)> Date.parse (今天)){
lblMsg.innerText = To Date应小于或等于Current日期;
document.getElementById( txtenddate)。focus();
return false ;
}
if (Date.parse(dtexp)> Date.parse (周一)){
lblMsg.innerText = From Date应小于或等于To日期;
document.getElementById( txtstartdate)。focus();
return false ;
}







aspx:

< asp: textbox id =txtbatchnamerunat =serverreadonly =truexmlns:asp =#unknown>

 <   asp:日历    ID   =  Calendar1    runat   =  server    OnSelectionChanged   =  Calendar1_SelectionChanged >  
< / asp:日历 >







代码背后:
------------
protected void Calendar1_SelectionChanged( object sender,EventArgs e)
{
txtstartdate.Text = Calendar1.SelectedDate.ToString();
}





u可以对文本框使用日历控件,如tis。

但是jquery datepicker是完成任务的简单方法


尝试在javascript中使用此功能,这是快速响应的最佳方式。



< pre lang =HTML> function ValidateEndDate(){
var startDate =


In the below code i need to add calender for two text Boxes
1. Start Date
2. End Date
and validate that StartDate should be lessthan EndDate.

I searched in Google i found the Code but they all are using HTML Controls but here i'm using Asp.Net Controls. Later i need to access those StartDate and EndDate in Codebehind.

<p>
  <asp:Label ID="lblcourses" runat="server" Text="Course Name"></asp:Label>
  <asp:DropDownList ID="drpcourses" runat="server">
  <asp:ListItem Text="--Select a Course--" Value="0"></asp:ListItem>
  <asp:ListItem Text="Basic Unix" Value="1"></asp:ListItem>
  <asp:ListItem Text="Basic Oracle" Value="2"></asp:ListItem>
  <asp:ListItem Text="PL/SQL" Value="3"></asp:ListItem>
  <asp:ListItem Text="Oracle 11g DBA" Value="4"></asp:ListItem>
  <asp:ListItem Text="Oracle 11g DBA F/T" Value="5"></asp:ListItem>
  <asp:ListItem Text="Oracle 11g RAC F/T" Value="6"></asp:ListItem>
  <asp:ListItem Text="Linux Admin" Value="7"></asp:ListItem>
  <asp:ListItem Text="Unix Administration" Value="8"></asp:ListItem>
  <asp:ListItem Text="Advance Shell" Value="9"></asp:ListItem>
  <asp:ListItem Text="Hadoop Administration" Value="10"></asp:ListItem>
  </asp:DropDownList>
  </p>

  <p>
  <asp:Label ID="lblbatchname" runat="server" Text="Batch Name"></asp:Label>
  <asp:TextBox ID="txtbatchname" runat="server" ReadOnly="true"></asp:TextBox>
  </p>

  <p>
  <asp:Label ID="lblstartdate" runat="server" Text="Start Date"></asp:Label>
  <asp:TextBox ID="txtstartdate" runat="server"></asp:TextBox>
  </p>

 <p>
  <asp:Label ID="lblenddate" runat="server" Text="End Date"></asp:Label>
  <asp:TextBox ID="txtenddate" runat="server"></asp:TextBox>
  </p>

<p>
<asp:Button ID="btnsave" runat="server"  Text="Add" OnClientClick="javascript:return ValidateDropDown();"/>
</p>

解决方案

Solution


Please don't use Plain TextBoxes for Date operations.

You can use jQuery DatePicker, which is simple to use.

Demo


[Demo] Date Comparision in JavaScript[^]


if u want to make use of javascript
try tis

function fnGo() {
                lblMsg.innerHTML = "";
                var Mon = new Date();
                var CurMonth = Mon.getMonth() + 1;
                var today = new Date();
                var dd = today.getDate();
                var mm = today.getMonth() + 1;
                var yyyy = today.getFullYear();
                if (dd < 10) { dd = '0' + dd } if (mm < 10) { mm = '0' + mm } var today = mm + '/' + dd + '/' + yyyy;

var fromdt=document.getElementById("txtstartdate").value;
var todt=document.getElementById("txtenddate").value;
                var dtexp = fromdt.split("-")[1] + '/' + fromdt.split("-")[0] + "/" + fromdt.split("-")[2];
                var Mon = todt .split("-")[1] + '/' + todt .split("-")[0] + "/" + fodt.split("-")[2];
                if (Date.parse(dtexp) > Date.parse(today)) {
                    lblMsg.innerText = "The From Date should be less than or equal to the Current Date";
                    document.getElementById("txtstartdate").focus();
                    return false;
                }
                if (Date.parse(Mon) > Date.parse(today)) {
                    lblMsg.innerText = "The To Date should be less than or equal to the Current Date";
                   document.getElementById("txtenddate").focus();
                    return false;
                }
                if (Date.parse(dtexp) > Date.parse(Mon)) {
                    lblMsg.innerText = "The From Date should be less than or equal to the To Date";
                    document.getElementById("txtstartdate").focus();
                    return false;
                }




aspx:
<asp:textbox id="txtbatchname" runat="server" readonly="true" xmlns:asp="#unknown">

<asp:Calendar ID="Calendar1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged">
    </asp:Calendar>




code behind:
------------
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
    txtstartdate.Text = Calendar1.SelectedDate.ToString();
}



u can use calendar control like tis for both textbox.
However jquery datepicker is an easy way to accomplish tis task


Try by using this function in javascript, the best way for fast response.

function ValidateEndDate() {
       var startDate =


这篇关于如何将Calender添加到文本框以及如何验证StartDate应该比EndDate更少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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