Form date choice validation

J

joeyej

I'm using this code to make sure users are unable to choose a date from
a drop list (<option value="June 8, 2006, (Thursday), 9am">June 12,
2006, (Thursday), 9am) that is less than two days out from the system
date. How can I improve it to be sure weekends are excluded? I also
need to make sure end of month/begin new month is included in choices
being at least 2 days away from system date - . Any help is
appreciated.

Thanks, Joe

FirstCh=document.choice.FirstCh.value
SecondCh=document.choice.SecondCh.value

FirstCh=document.choice.FirstCh.value

function makeArray2() {
var args = makeArray1.arguments;
for (var i = 0; i < args.length; i++) {
this = args;
}
this.length = args.length;
}

function fixDate2(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}

function getString2(date) {
var months = new makeArray2("January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December");
return months[date.getMonth()] + " " +
(date.getDate() + 2) + ", " +
date.getFullYear() + "," + " ";
}
var cur2 = new Date();
fixDate2(cur2);
var today2 = getString2(cur2);

var fCD = FirstCh.substring (FirstCh.indexOf (">"),
FirstCh.indexOf("("));
if (fCD == today2) {
alert("Delivery date choice must be at least two days from today.")
document.laptop.FirstCh.focus()
return false
}
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Thu, 8 Jun 2006 11:51:33 remote, seen in
news:comp.lang.javascript said:
I'm using this code to make sure users are unable to choose a date from
a drop list (<option value="June 8, 2006, (Thursday), 9am">June 12,
2006, (Thursday), 9am) that is less than two days out from the system
date.

Then put only acceptable dates in the list, if you can assume javascript
is enabled or if the page is generated by server code. And June 12 is
expected to be a Monday.

"Two days out" is unclear - is it inclusive or exclusive, forwards or
backwards or both? If an order is placed on Monday at 23:59, is it
reasonable to promise the same delivery as for an order placed on Monday
at 00:01? What if the customer is in a different time zone to the
supplier?

How can I improve it to be sure weekends are excluded?

You must first define "weekend"; it varies between countries and
religions.
I also
need to make sure end of month/begin new month is included in choices
being at least 2 days away from system date - . Any help is
appreciated.

Superfluous.


function makeArray2() {
var args = makeArray1.arguments;

makeArray1 is not known.
for (var i = 0; i < args.length; i++) {
this = args;
}
this.length = args.length;
}


The function seems pointless; what do you intend it to do, and why do
you think that is needed?
function fixDate2(date) {
var base = new Date(0);
var skew = base.getTime();
if (skew > 0)
date.setTime(date.getTime() - skew);
}

(a) you are not allowing for skew possibly being negative?
(b) skew will always be zero.
function getString2(date) {
var months = new makeArray2("January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December");

You only need var months = ['Jan', ..., 'Dec']
return months[date.getMonth()] + " " +
(date.getDate() + 2) + ", " +
date.getFullYear() + "," + " ";
}
var cur2 = new Date();
fixDate2(cur2);
var today2 = getString2(cur2);

var fCD = FirstCh.substring (FirstCh.indexOf (">"),
FirstCh.indexOf("("));
if (fCD == today2) {
alert("Delivery date choice must be at least two days from today.")
document.laptop.FirstCh.focus()
return false
}

Before writing javascript, you should first learn something about it.
Before coding for dates, you should first learn something about the
facilities provided in the language.
Code should be indented to show structure.
Comment is important in non-working code, as it can show intent.
ISTM that you should have paid more attention in your classes.

This will show a list of future dates matching the criteria within a
given number of days; to get a list of given length, move N++ -

D = new Date() // if (D.getHours()>=12) D.setDate(D.getDate()+1)
for (D.setDate(D.getDate()+2), N=0; N<22; D.setDate(D.getDate()+1), N++)
if (D.getDay()%6!=0) document.writeln(D, '<br>')

You will need to format that date and to add it to the options array of
the selector - see js-date7.htm#WeDA .

Read the newsgroup FAQ; see below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top