Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Form date choice validation

Reply
Thread Tools

Form date choice validation

 
 
joeyej
Guest
Posts: n/a
 
      06-08-2006
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[i] = args[i];
}
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
}

 
Reply With Quote
 
 
 
 
Dr John Stockton
Guest
Posts: n/a
 
      06-09-2006
JRS: In article < .com>
, dated Thu, 8 Jun 2006 11:51:33 remote, seen in
news:comp.lang.javascript, joeyej <>
posted :
>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.


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


Superfluous.


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


makeArray1 is not known.

> for (var i = 0; i < args.length; i++) {
> this[i] = args[i];
> }
> 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.

--
© 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ as choice for long term application choice. miles.jg C++ 16 11-14-2007 03:43 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Form Validation Problem...Persisiting form fields on validation failure. bnp Javascript 4 05-12-2004 12:16 PM
Can Choice components respond to keyboard input like HTML Choice components? Mickey Segal Java 0 02-02-2004 10:59 PM
Choice of DHCP-server? Is the "IOS-one" a good choice? Fred Cisco 1 12-11-2003 06:25 AM



Advertisments