473,246 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

how to compare current date to a future date and validate it in ASP.NET

Hello there,

In my asp.net page using VB, I have a date text field in mm/dd/yyyy
format. When a date is entered, I'd like to validate it to make sure
the date is greater than or equal to the current date. If not, I'd
like to display the error message to ValidationSummary.

It seems to make sense to me to use CompareValidator but the problem
is put the current date into CompareValidator. So, I created a hidden
text field in my aspx. In my VB code behind, I load current date to
that text field. Then, I created a CompareValidator to compare this
current date to the text field containing date entered by the user.
In my Page_Load, I have Page.Validate to activate the validation.
When I run it, the first round through, the validationSummary worked
and displayed the warning message I wanted (that date entered can't be
less than current date). However, it did not work after that. I
think the problem is server validation did not work properly.

My alternative way is to use client validation using CustomValidator.
Pass in two date fields: current date and entered date and compare.
Unfortunately, I don't know well neither javaScrip nor any other
script languages to do it. The problem I am having with this approach
is getting current date in the mm/dd/yyyy format so I can compare to
what the user enters in that format. Date() function is javascrip
returns a different format. And I think Now() returns both date and
time.

Any suggestions are greatly appreciated. Thank you in advance,
James
Nov 18 '05 #1
7 31708
Hi,

yes you'd use CompareValidator, but you need to give it the value as
correctly formatted to the ValueToCompare property.

First you'd put the validators something like this (I've omitted buttons and
ValidationSummary to keep it concise):

***
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

<asp:CompareValidator id="compDateDataTypeValidator"
ControlToValidate="TextBox1" Operator="DataTypeCheck"
Type="Date" runat="server" ErrorMessage="You must enter a valid
date"></asp:CompareValidator>

<asp:CompareValidator id="compDateValidator"
ControlToValidate="TextBox1" Operator="LessThan" Type="Date"
runat="server" ErrorMessage="Entered date must be less than current
date"></asp:CompareValidator>
***

Then additionally you'd set in code the ValueToCompare property for the
CompareValidator (compDateValidator) which compares the dates. Like this:

***
compDateValidator.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
***
As you can see, it is given as string and the easiest way is to get it from
DateTime.Now and then format using string formatting so that it passes for
CompareValidator (standard date format based on current locale and culture)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke



"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
Hello there,

In my asp.net page using VB, I have a date text field in mm/dd/yyyy
format. When a date is entered, I'd like to validate it to make sure
the date is greater than or equal to the current date. If not, I'd
like to display the error message to ValidationSummary.

It seems to make sense to me to use CompareValidator but the problem
is put the current date into CompareValidator. So, I created a hidden
text field in my aspx. In my VB code behind, I load current date to
that text field. Then, I created a CompareValidator to compare this
current date to the text field containing date entered by the user.
In my Page_Load, I have Page.Validate to activate the validation.
When I run it, the first round through, the validationSummary worked
and displayed the warning message I wanted (that date entered can't be
less than current date). However, it did not work after that. I
think the problem is server validation did not work properly.

My alternative way is to use client validation using CustomValidator.
Pass in two date fields: current date and entered date and compare.
Unfortunately, I don't know well neither javaScrip nor any other
script languages to do it. The problem I am having with this approach
is getting current date in the mm/dd/yyyy format so I can compare to
what the user enters in that format. Date() function is javascrip
returns a different format. And I think Now() returns both date and
time.

Any suggestions are greatly appreciated. Thank you in advance,
James

Nov 18 '05 #2
Sorry, I re-read your post and you said date format dd.mm.yyyy. TThen just
change the format string to "dd/MM/yyyy" (or dd.MM.yyyy). /'s are considered
culture-speficic separators to dates and will be replaced with dots.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
Hi,

yes you'd use CompareValidator, but you need to give it the value as
correctly formatted to the ValueToCompare property.

First you'd put the validators something like this (I've omitted buttons and ValidationSummary to keep it concise):

***
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

<asp:CompareValidator id="compDateDataTypeValidator"
ControlToValidate="TextBox1" Operator="DataTypeCheck"
Type="Date" runat="server" ErrorMessage="You must enter a valid
date"></asp:CompareValidator>

<asp:CompareValidator id="compDateValidator"
ControlToValidate="TextBox1" Operator="LessThan" Type="Date"
runat="server" ErrorMessage="Entered date must be less than current
date"></asp:CompareValidator>
***

Then additionally you'd set in code the ValueToCompare property for the
CompareValidator (compDateValidator) which compares the dates. Like this:

***
compDateValidator.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
***
As you can see, it is given as string and the easiest way is to get it from DateTime.Now and then format using string formatting so that it passes for
CompareValidator (standard date format based on current locale and culture)
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke



"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
Hello there,

In my asp.net page using VB, I have a date text field in mm/dd/yyyy
format. When a date is entered, I'd like to validate it to make sure
the date is greater than or equal to the current date. If not, I'd
like to display the error message to ValidationSummary.

It seems to make sense to me to use CompareValidator but the problem
is put the current date into CompareValidator. So, I created a hidden
text field in my aspx. In my VB code behind, I load current date to
that text field. Then, I created a CompareValidator to compare this
current date to the text field containing date entered by the user.
In my Page_Load, I have Page.Validate to activate the validation.
When I run it, the first round through, the validationSummary worked
and displayed the warning message I wanted (that date entered can't be
less than current date). However, it did not work after that. I
think the problem is server validation did not work properly.

My alternative way is to use client validation using CustomValidator.
Pass in two date fields: current date and entered date and compare.
Unfortunately, I don't know well neither javaScrip nor any other
script languages to do it. The problem I am having with this approach
is getting current date in the mm/dd/yyyy format so I can compare to
what the user enters in that format. Date() function is javascrip
returns a different format. And I think Now() returns both date and
time.

Any suggestions are greatly appreciated. Thank you in advance,
James


Nov 18 '05 #3
"Teemu Keiski" <jo****@aspalliance.com> wrote in message news:<uf**************@TK2MSFTNGP09.phx.gbl>...
Sorry, I re-read your post and you said date format dd.mm.yyyy. TThen just
change the format string to "dd/MM/yyyy" (or dd.MM.yyyy). /'s are considered
culture-speficic separators to dates and will be replaced with dots.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
Hi,

yes you'd use CompareValidator, but you need to give it the value as
correctly formatted to the ValueToCompare property.

First you'd put the validators something like this (I've omitted buttons

and
ValidationSummary to keep it concise):

***
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

<asp:CompareValidator id="compDateDataTypeValidator"
ControlToValidate="TextBox1" Operator="DataTypeCheck"
Type="Date" runat="server" ErrorMessage="You must enter a valid
date"></asp:CompareValidator>

<asp:CompareValidator id="compDateValidator"
ControlToValidate="TextBox1" Operator="LessThan" Type="Date"
runat="server" ErrorMessage="Entered date must be less than current
date"></asp:CompareValidator>
***

Then additionally you'd set in code the ValueToCompare property for the
CompareValidator (compDateValidator) which compares the dates. Like this:

***
compDateValidator.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
***
As you can see, it is given as string and the easiest way is to get it

from
DateTime.Now and then format using string formatting so that it passes for
CompareValidator (standard date format based on current locale and

culture)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke



"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
Hello there,

In my asp.net page using VB, I have a date text field in mm/dd/yyyy
format. When a date is entered, I'd like to validate it to make sure
the date is greater than or equal to the current date. If not, I'd
like to display the error message to ValidationSummary.

It seems to make sense to me to use CompareValidator but the problem
is put the current date into CompareValidator. So, I created a hidden
text field in my aspx. In my VB code behind, I load current date to
that text field. Then, I created a CompareValidator to compare this
current date to the text field containing date entered by the user.
In my Page_Load, I have Page.Validate to activate the validation.
When I run it, the first round through, the validationSummary worked
and displayed the warning message I wanted (that date entered can't be
less than current date). However, it did not work after that. I
think the problem is server validation did not work properly.

My alternative way is to use client validation using CustomValidator.
Pass in two date fields: current date and entered date and compare.
Unfortunately, I don't know well neither javaScrip nor any other
script languages to do it. The problem I am having with this approach
is getting current date in the mm/dd/yyyy format so I can compare to
what the user enters in that format. Date() function is javascrip
returns a different format. And I think Now() returns both date and
time.

Any suggestions are greatly appreciated. Thank you in advance,
James



Teemu,

Thanks a lot for responding and trying to help me. Yes, you got it
right the first time with "mm/dd/yyyy" as I would like. I used your
suggestion and I got "The value 'DateTime.Now.ToString("mm/dd/yyyy")'
of the ValueToCompare property of 'cvDate' cannot be converted to type
'Date'". Any other suggestions are appreciated.

James
Nov 18 '05 #4
If you see my VB code, it goes through compilation. You have a syntax typo
there. What's *exactly* the code you use for this line?

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
"Teemu Keiski" <jo****@aspalliance.com> wrote in message

news:<uf**************@TK2MSFTNGP09.phx.gbl>...
Sorry, I re-read your post and you said date format dd.mm.yyyy. TThen just change the format string to "dd/MM/yyyy" (or dd.MM.yyyy). /'s are considered culture-speficic separators to dates and will be replaced with dots.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:eE**************@TK2MSFTNGP10.phx.gbl...
Hi,

yes you'd use CompareValidator, but you need to give it the value as
correctly formatted to the ValueToCompare property.

First you'd put the validators something like this (I've omitted buttons
and
ValidationSummary to keep it concise):

***
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>

<asp:CompareValidator id="compDateDataTypeValidator"
ControlToValidate="TextBox1" Operator="DataTypeCheck"
Type="Date" runat="server" ErrorMessage="You must enter a valid
date"></asp:CompareValidator>

<asp:CompareValidator id="compDateValidator"
ControlToValidate="TextBox1" Operator="LessThan" Type="Date"
runat="server" ErrorMessage="Entered date must be less than

current date"></asp:CompareValidator>
***

Then additionally you'd set in code the ValueToCompare property for the CompareValidator (compDateValidator) which compares the dates. Like this:
***
compDateValidator.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
***
As you can see, it is given as string and the easiest way is to get it

from
DateTime.Now and then format using string formatting so that it passes for CompareValidator (standard date format based on current locale and

culture)

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke



"James P." <ha*********@yahoo.com> wrote in message
news:f4**************************@posting.google.c om...
> Hello there,
>
> In my asp.net page using VB, I have a date text field in mm/dd/yyyy
> format. When a date is entered, I'd like to validate it to make sure > the date is greater than or equal to the current date. If not, I'd
> like to display the error message to ValidationSummary.
>
> It seems to make sense to me to use CompareValidator but the problem
> is put the current date into CompareValidator. So, I created a hidden > text field in my aspx. In my VB code behind, I load current date to > that text field. Then, I created a CompareValidator to compare this
> current date to the text field containing date entered by the user.
> In my Page_Load, I have Page.Validate to activate the validation.
> When I run it, the first round through, the validationSummary worked
> and displayed the warning message I wanted (that date entered can't be > less than current date). However, it did not work after that. I
> think the problem is server validation did not work properly.
>
> My alternative way is to use client validation using CustomValidator. > Pass in two date fields: current date and entered date and compare.
> Unfortunately, I don't know well neither javaScrip nor any other
> script languages to do it. The problem I am having with this approach > is getting current date in the mm/dd/yyyy format so I can compare to
> what the user enters in that format. Date() function is javascrip
> returns a different format. And I think Now() returns both date and
> time.
>
> Any suggestions are greatly appreciated. Thank you in advance,
> James


Teemu,

Thanks a lot for responding and trying to help me. Yes, you got it
right the first time with "mm/dd/yyyy" as I would like. I used your
suggestion and I got "The value 'DateTime.Now.ToString("mm/dd/yyyy")'
of the ValueToCompare property of 'cvDate' cannot be converted to type
'Date'". Any other suggestions are appreciated.

James

Nov 18 '05 #5


Teemu,

Here is what I used:
<asp:CompareValidator id="cvCompareDate" style="Z-INDEX: 115; LEFT:
456px; POSITION: absolute; TOP: 180px"
runat="server" ControlToValidate="txtFutureEventDate"
ErrorMessage="toot toot" Operator="GreaterThanEqual"
Type="Date"
ValueToCompare='DateTime.Now.ToString("MM/dd/yyyy")'></asp:CompareValida
tor>

I even tried ("M/d/yyyy"). I got the same error message like I describe
earlier.

James

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #6
You can't assign the value on inline code that way. I used it in
code-behind, but anyway, it should be in code (in your case put code in
Page_Load, for example) to use it the way I demonstrated

*** With the code ***
cvCompareDate.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy")
******************

Another way with inline way would be with binding. e.g set the attribute
like this:

***With inline ***
ValueToCompare='<%#DateTime.Now.ToString("MM/dd/yyyy")%>'
***************

Additionally you would need to call DataBind for the CompareValidator
control in question (or if suits, for the whole Page at the same time with
Page.DataBind() ) to get this way to work
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke

"phamtasmic" <ha*********@yahoo.com> wrote in message
news:ud**************@TK2MSFTNGP12.phx.gbl...


Teemu,

Here is what I used:
<asp:CompareValidator id="cvCompareDate" style="Z-INDEX: 115; LEFT:
456px; POSITION: absolute; TOP: 180px"
runat="server" ControlToValidate="txtFutureEventDate"
ErrorMessage="toot toot" Operator="GreaterThanEqual"
Type="Date"
ValueToCompare='DateTime.Now.ToString("MM/dd/yyyy")'></asp:CompareValida
tor>

I even tried ("M/d/yyyy"). I got the same error message like I describe
earlier.

James

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #7
Teemu,

You are right about that. I put that code in the Init event for the
textbox to get the current date and it worked.

Thanks a lot for helping,
James
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: asreryll | last post by:
I wish to show a future date in a table, so that I can sort and know which case is in need of a review. I can display a future date in a form but I want to see all records that are due on a certain...
28
by: ROSY | last post by:
1.How to make self deletable .exe file such that during runtime the file delete itself from the current path. plz answer, thanks in advence. bye.
7
by: joeyej | last post by:
How do I compare today's date with this string (in my inc file) so that I can set an alert if date choice i.e. May 15, 2006 not at least greater than two days from current date? <option...
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
5
by: Kevin | last post by:
I have a simple application that handles authentication, and one of the things it checks is password aging. If I have something like this: If ("02/14/2007" <= Date.Today.ToString("MM/dd/yyyy"))...
2
by: bloukopkoggelmander | last post by:
Hi all Right, I dont know if this is possible in Access (2007) or not but here goes. I want to create a form for a vehicle delivery where a user completes say half of it before delivery of the...
2
by: =?Utf-8?B?Sm9ubnk=?= | last post by:
I have an ASP.NET 2.0 C# web application that is contacting an Exchange server using WEBDAV. It allows the users to look up appointments for a future date. The problem I have is determining the...
1
by: dkyadav80 | last post by:
Hi Sir, I'm begener in Ajax or Javascript.I have problem about date validation. I have 3 field in html page: 1->date(numeric), 2->month(text 1st 3 letter) 3->year(numeric). I wanna implement to...
4
by: drrajnishpatel via AccessMonster.com | last post by:
Dear Friends i am trying to get a future date as follows, data:1) date of proceeding to leave = D 2) number of days leave sanctioned =N 3) date of reporting for duty =R i grant "n" number of...
7
by: jmartmem | last post by:
Greetings, I have an ASP page with a form (form1) that contains a JavaScript validation function that is activated onSubmit. The function contains a series of IF statements to alert the user to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.