Form date choice validation

Discussion in 'Javascript' started by joeyej, Jun 8, 2006.

  1. joeyej

    joeyej Guest

    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
    }
    joeyej, Jun 8, 2006
    #1
    1. Advertising

  2. JRS: In article <>
    , 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 = 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.

    --
    © 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.
    Dr John Stockton, Jun 9, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abc
    Replies:
    1
    Views:
    410
    Aytaç ÖZAY
    Apr 7, 2006
  2. Mickey Segal
    Replies:
    0
    Views:
    857
    Mickey Segal
    Feb 2, 2004
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,229
    Michael Borgwardt
    May 30, 2004
  4. miles.jg
    Replies:
    16
    Views:
    858
    Alf P. Steinbach
    Nov 14, 2007
  5. bnp
    Replies:
    4
    Views:
    301
Loading...

Share This Page