By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,247 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,247 IT Pros & Developers. It's quick & easy.

Validating 2 date fields

P: n/a
Hi All

I have a <SELECT> for the month (1 .. 12) and a <SELECT> for the year (2004
.... 2020), do you know of any js validation check I can use to check whether
these values are older than today's date so that I can bring up a warning,
eg

user chooses 04 and 2003 or 01 and 2004

OnClick a validate script sees that either of the above are older than
today's date and brings up an alert/doesn't submit the form.

Any ideas?

Rgds

Robbie


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Astra wrote:
Hi All

I have a <SELECT> for the month (1 .. 12) and a <SELECT> for the year (2004
... 2020), do you know of any js validation check I can use to check whether
these values are older than today's date so that I can bring up a warning,
eg


Do a search on "Re: Date format dd/mm" and a post by Mike Winter on
8 Oct. That, and Dr John Stockton's followup post, should answer any
question you may have regarding dates - oh, and look at the FAQ:

<URL:http://www.jibbering.com/faq/>

Dr. J also suggests looking at;

<URL:http://www.merlyn.demon.co.uk/js-index.htm>

Fred.
Jul 23 '05 #2

P: n/a
Thanks Fred
"Fred Oz" <oz****@iinet.net.auau> wrote in message
news:41***********************@per-qv1-newsreader-01.iinet.net.au...
Astra wrote:
Hi All

I have a <SELECT> for the month (1 .. 12) and a <SELECT> for the year
(2004
... 2020), do you know of any js validation check I can use to check
whether
these values are older than today's date so that I can bring up a warning,
eg


Do a search on "Re: Date format dd/mm" and a post by Mike Winter on
8 Oct. That, and Dr John Stockton's followup post, should answer any
question you may have regarding dates - oh, and look at the FAQ:

<URL:http://www.jibbering.com/faq/>

Dr. J also suggests looking at;

<URL:http://www.merlyn.demon.co.uk/js-index.htm>

Fred.


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 23 '05 #3

P: n/a
On Wed, 27 Oct 2004 12:03:12 +0100, Astra <in**@NoEmail.com> wrote:
I have a <SELECT> for the month (1 .. 12) and a <SELECT> for the year
(2004 ... 2020), do you know of any js validation check I can use to
check whether these values are older than today's date so that I can
bring up a warning,


You can create two Date objects. The first will have the current date,
whilst the second will be a copy modified to the selected month and year.
A simple comparison with then determine if the input is before the current
month.

/* This assumes that each OPTION element has a number with months
* being 1 to 12, and years being four digits.
*/
function isBeforeDate(form) {
/* The Date object uses zero-order month numbers, hence the -1. */
var month = +form.elements['monthSelect'].value - 1,
year = +form.elements['yearSelect'].value,
now = new Date(),
then = new Date(now);

then.setFullYear(year, month);

if(then < now) {
// The selected month/year is before the current month/year.
}
return (then < now);
}

<!-- Cancels the submission is the date is before this month. -->
<form ... onsubmit="return !isBeforeDate(this);">

Of course, you can integrate the validation into an existing submission
handler, but the check should only be done at this point. If you did it
when the SELECT elements are changed, you might catch the user in the
middle of changing the date to an acceptable date, doing nothing but
causing frustration.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4

P: n/a
JRS: In article <41********@127.0.0.1>, dated Wed, 27 Oct 2004
12:03:12, seen in news:comp.lang.javascript, Astra <in**@NoEmail.com>
posted :
I have a <SELECT> for the month (1 .. 12) and a <SELECT> for the year (2004
... 2020), do you know of any js validation check I can use to check whether
these values are older than today's date so that I can bring up a warning,
eg

user chooses 04 and 2003 or 01 and 2004

OnClick a validate script sees that either of the above are older than
today's date and brings up an alert/doesn't submit the form.

You may require to check more than one attempt during a given actual
month. You appear to guarantee field strings YYYY & MM. Therefore, it
is more effective to compare strings of the form YYYYMM.
with (new Date()) Now = String(getFullYear()*100 + getMonth() + 1)

if (YrField + MoField < Now) { ... // or <=
Creating unnecessary Date Objects wastes microseconds.

If getFullYear() is not with certainty available, as a short-term
solution you can use 2000 + getYear()%100 .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #5

P: n/a
On Wed, 27 Oct 2004 22:56:41 +0100, Dr John Stockton
<sp**@merlyn.demon.co.uk> wrote:

[snip]
You appear to guarantee field strings YYYY & MM. Therefore, it is more
effective to compare strings of the form YYYYMM.
Indeed. I hadn't considered that (though it's quite an obvious solution).

[snip]
Creating unnecessary Date Objects wastes microseconds.
Considering that the only reasonable time to perform this validation is at
form submission, even a delay of an entire second is meaningless.
If getFullYear() is not with certainty available, [...]


This thought (or rather one concerning setFullYear) also crossed my mind,
so I decided to see when Netscape and Microsoft implemented setFullYear:
NN 4.06 and IE 4, respectively. I would assume that other browser vendors
implemented the method, and other similar ones, around that time.

Should the presence of set/getFullYear still remain a concern?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.