Two input fields to a single database field

Discussion in 'Javascript' started by dlrider, Sep 7, 2007.

  1. dlrider

    dlrider Guest

    Hello,
    I don't know what this would be called, and searches for the subject
    above are not useful. FAQ did not seem to provide anything either.
    My inherited application already uses Javascript for data input
    validations so I thought this would be a good fit for it also.

    Here's my situation:
    I have a form (below) with a date input to filter with on a single
    database field. I need to allow mM/dD/YYYY input (which already
    works) and number-of-days-prior -- user's choice. This is supposed to
    filter items that have a datestamp n number of days prior to today's
    current date OR prior to the date entered. The end criteria for
    either is formatted as the date mm/dd/yyyy.

    Can Javascript help me with this requirement? What is this called? So
    maybe I can search further for existing posts or web pages.

    Thanks in advance.

    dlrider


    form code:
    <form name="AdminFind" action="index.cfm?action=adminfindlist"
    method="post">
    Plan modified on/before date:
    <input name="plan_main_user_modify_date" type="text" size=10
    maxlength=10 value="">
    <input name="find" type="submit" value="Find" onClick="return
    validateDate();" >
    </form>
    dlrider, Sep 7, 2007
    #1
    1. Advertising

  2. dlrider

    Matt Ratliff Guest

    Hey dlrider,
    See if this is what you are looking for.

    <script language="javascript">
    function addtodate(date1, numberdays)
    {
    newdate=new Date(date1.getTime()+numberdays*(1000*60*60*24));
    return newdate;
    }
    mydate=addtodate(new Date(),3);
    alert(mydate);
    </script>

    This should add "numberdays" to the date passed to the function. You
    can modify it to subtract as well. You might have to do some
    formatting on the mm/dd/yyyy to create the date object to pass to the
    function.

    Matt R.


    On Fri, 07 Sep 2007 12:46:59 -0700, dlrider <>
    wrote:

    >Hello,
    >I don't know what this would be called, and searches for the subject
    >above are not useful. FAQ did not seem to provide anything either.
    >My inherited application already uses Javascript for data input
    >validations so I thought this would be a good fit for it also.
    >
    >Here's my situation:
    >I have a form (below) with a date input to filter with on a single
    >database field. I need to allow mM/dD/YYYY input (which already
    >works) and number-of-days-prior -- user's choice. This is supposed to
    >filter items that have a datestamp n number of days prior to today's
    >current date OR prior to the date entered. The end criteria for
    >either is formatted as the date mm/dd/yyyy.
    >
    >Can Javascript help me with this requirement? What is this called? So
    >maybe I can search further for existing posts or web pages.
    >
    >Thanks in advance.
    >
    >dlrider
    >
    >
    >form code:
    ><form name="AdminFind" action="index.cfm?action=adminfindlist"
    >method="post">
    >Plan modified on/before date:
    ><input name="plan_main_user_modify_date" type="text" size=10
    >maxlength=10 value="">
    ><input name="find" type="submit" value="Find" onClick="return
    >validateDate();" >
    ></form>
    Matt Ratliff, Sep 7, 2007
    #2
    1. Advertising

  3. dlrider

    ASM Guest

    En réponse à dlrider qui écrivit, en date du : 7/09/07 21:46, le
    message suivant :
    > Hello,
    > I don't know what this would be called, and searches for the subject
    > above are not useful. FAQ did not seem to provide anything either.
    > My inherited application already uses Javascript for data input
    > validations so I thought this would be a good fit for it also.
    >
    > Here's my situation:
    > I have a form (below) with a date input to filter with on a single
    > database field. I need to allow mM/dD/YYYY input (which already
    > works) and number-of-days-prior -- user's choice. This is supposed to
    > filter items that have a datestamp n number of days prior to today's
    > current date OR prior to the date entered. The end criteria for
    > either is formatted as the date mm/dd/yyyy.
    >
    > Can Javascript help me with this requirement? What is this called? So
    > maybe I can search further for existing posts or web pages.
    >
    > Thanks in advance.
    >
    > dlrider
    >
    >
    > form code:
    > <form name="AdminFind" action="index.cfm?action=adminfindlist"
    > method="post">
    > Plan modified on/before date:
    > <input name="plan_main_user_modify_date" type="text" size=10
    > maxlength=10 value="">
    > <input name="find" type="submit" value="Find" onClick="return
    > validateDate();" >
    > </form>
    >


    <form name="AdminFind" action="index.cfm" method="post"
    onsubmit="return validateDate(this);">
    <input type="hidden" name="action" value="adminfindlist">
    Plan modified on/before date:
    <input name="plan_main_user_modify_date" type="text" size=10
    maxlength=10 value="">
    <input type="submit" value="Find">
    </form>

    <script type="text/javascript">
    function validateDate(where) {
    // the PHP gives the actual or required date
    var day = new Date(<? echo "$thisYear,$thisMonth,$thisDay"; ?>);
    var user_date = document.forms[where].plan_main_user_modify_date;
    var user_day = user_date.value;
    // verif entry
    if(user_day.length!=10 ||
    user_day.replace(/\//g,'').length!=8 ||
    !Number(user_day.replace(/\//g,''))
    ) {
    alert('empty field or bad date (dd/mm/yyyy)'+
    '\nchamp vide ou format incorrect (jj/mm/aaaa)');
    user_date.focus();
    user_date.select();
    return false;
    }

    // here we are
    user_day = user_day.split('/');
    user_day = new Date(user_day[2],user_day[1],user_day[0]);
    // time spent since 1970 for user's day
    user_day = Date.parse(user_day);
    // time spent since 1970 for required day
    day = Date.parse(day);
    // validation
    if((day_user>0 && day<user_day) ||
    (day<0 && user_day<0 && day>user_day) )
    {
    alert('your date is after allowed one');
    user_date.focus();
    user_date.select();
    return false;
    }
    return true;
    }
    </script>


    --
    sm
    ASM, Sep 8, 2007
    #3
  4. In comp.lang.javascript message <7dc3e394jrrq4ed5a4f54akaokivs04cce@4ax.
    com>, Fri, 7 Sep 2007 16:29:20, Matt Ratliff <>
    posted:

    > newdate=new Date(date1.getTime()+numberdays*(1000*60*60*24));


    Shoddy coding, but will pass simple-minded testing. OK if used only in
    places like Beijing, Accra, Hawaii.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr J R Stockton, Sep 8, 2007
    #4
  5. In comp.lang.javascript message <46e1e2e1$0$25941$
    r>, Sat, 8 Sep 2007 01:44:50, ASM <
    valid> posted:

    >user_day = user_day.split('/');

    probably OK

    >user_day = new Date(user_day[2],user_day[1],user_day[0]);

    A -1 is usually needed here ................^

    >// time spent since 1970 for user's day

    No, that would mean 86400 seconds for the start of 1971-01-02 local.
    You mean "time in milliseconds since 1970-01-01 00:00:00 UTC ..."

    >user_day = Date.parse(user_day);

    Initially, user_day was a Date Object; Date.parse requires a String, so
    implicit conversion is needed; Date.parse will then convert back (and
    would get it wrong for most of the First Century AD).
    user_day = +user_day // will convert from Date Object to Number,
    // reliably and rapidly.


    If one has two valid string dates for similar locations and possibly
    dissimilar seasons, and the time parts are absent, zero, or similar,
    then
    DaysDifference = Math.round( ( new Date(DS2)-new Date(DS1) )/864e5 )

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <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 J R Stockton, Sep 8, 2007
    #5
  6. dlrider

    ASM Guest

    En réponse à Dr J R Stockton qui écrivit, en date du : 8/09/07 22:36,
    le message suivant :
    > In comp.lang.javascript message <46e1e2e1$0$25941$
    > r>, Sat, 8 Sep 2007 01:44:50, ASM <
    > valid> posted:
    >
    >> user_day = user_day.split('/');

    > probably OK


    Don't know ... but it works :)
    (didn't try with the part of php code)

    >> user_day = new Date(user_day[2],user_day[1],user_day[0]);

    > A -1 is usually needed here ................^


    oops !?

    >> // time spent since 1970 for user's day

    > No, that would mean 86400 seconds for the start of 1971-01-02 local.
    > You mean "time in milliseconds since 1970-01-01 00:00:00 UTC ..."


    OK, thanks for the precision.

    >> user_day = Date.parse(user_day);

    > Initially, user_day was a Date Object; Date.parse requires a String, so
    > implicit conversion is needed;


    ?? Firefox's Errors consol didn't tell me that was wrong.
    I thought that new Date() returned a string, no?

    > Date.parse will then convert back (and
    > would get it wrong for most of the First Century AD).


    I hope there will be no meeting planed at this time :)

    > user_day = +user_day // will convert from Date Object to Number,
    > // reliably and rapidly.


    You're not wrong.
    but isn't automatically converted by the condition (day_user>0) ?

    > If one has two valid string dates for similar locations and possibly
    > dissimilar seasons, and the time parts are absent, zero, or similar,
    > then
    > DaysDifference = Math.round( ( new Date(DS2)-new Date(DS1) )/864e5 )


    Great !
    Remain only to try to remember it ... !

    --
    sm
    ASM, Sep 10, 2007
    #6
  7. dlrider

    Matt Ratliff Guest

    What, and it took you a whole day to come up with this all by
    yourself. I guess if everyone had that much time we might be out of
    the dark ages by now. :)

    On Sat, 8 Sep 2007 20:42:12 +0100, Dr J R Stockton
    <> wrote:

    >In comp.lang.javascript message <7dc3e394jrrq4ed5a4f54akaokivs04cce@4ax.
    >com>, Fri, 7 Sep 2007 16:29:20, Matt Ratliff <>
    >posted:
    >
    >> newdate=new Date(date1.getTime()+numberdays*(1000*60*60*24));

    >
    >Shoddy coding, but will pass simple-minded testing. OK if used only in
    >places like Beijing, Accra, Hawaii.
    Matt Ratliff, Sep 10, 2007
    #7
  8. dlrider

    dlrider Guest

    Thank you all for your responses. However, I am not as intelligent
    RE: javascript and could not get these to work. In digging through
    the Web for things related I came across this (http://
    www.thescripts.com/forum/thread91233.html):

    <script type="text/javascript">
    function zp(n){
    return n<10?("0"+n):n;
    }
    function insertDate(t,format){
    var now=new Date();
    var DD=zp(now.getDate());
    var MM=zp(now.getMonth()+1);
    var YYYY=now.getFullYear();
    var YY=zp(now.getFullYear()%100);
    format=format.replace(/DD/,DD);
    format=format.replace(/MM/,MM);
    format=format.replace(/YYYY/,YYYY);
    format=format.replace(/YY/,YY);
    t.value=format;
    }
    </script>

    This would work for our purposes, except I would like it to bump the
    month back one by default. I found how to do that but when the
    current date is January the month becomes "00". How would I get it to
    bump the month to "12" and then the year back one? (I can see now
    that I will be more of a plug-n-pray type javascript user). I
    envision a nested "if-then" series to do this but I have spun my
    wheels doing this before. Thanks again.

    D.
    dlrider, Sep 10, 2007
    #8
  9. In comp.lang.javascript message <46e48ee1$0$5075$
    >, Mon, 10 Sep 2007 02:23:11, ASM <

    valid> posted:
    >En réponse à Dr J R Stockton qui écrivit, en date du : 8/09/07 22:36,
    >le message suivant :
    >> In comp.lang.javascript message <46e1e2e1$0$25941$
    >> r>, Sat, 8 Sep 2007 01:44:50, ASM <
    >> valid> posted:



    >>> // time spent since 1970 for user's day

    >> No, that would mean 86400 seconds for the start of 1971-01-02 local.
    >> You mean "time in milliseconds since 1970-01-01 00:00:00 UTC ..."

    >
    >OK, thanks for the precision.


    You do, after all, seem to be of the country in which BIPM and BIH and
    IERS reside ... . But many in the obtuser parts of the world ignore the
    difference, and suffer, and cause others to suffer too.


    >>> user_day = Date.parse(user_day);

    >> Initially, user_day was a Date Object; Date.parse requires a String, so
    >> implicit conversion is needed;

    >
    >?? Firefox's Errors consol didn't tell me that was wrong.


    It's valid. It's as valid as going from Paris to Versailles via Monaco.

    Another tip for speed : the UTC functions are considerably faster than
    the non-UTC ones; only use non-UTC when handling actual date/time,
    orneeding to respect time zones or Summer Time.


    >I thought that new Date() returned a string, no?


    No. It returns a Date Object, which holds an IEEE Double of
    milliseconds from epoch and has several Methods. One of those,
    toString, is used by default if the context demands a string.

    >> Date.parse will then convert back (and
    >> would get it wrong for most of the First Century AD).

    >
    >I hope there will be no meeting planed at this time :)


    But it should be considered when coding for the Date of Easter!

    >> user_day = +user_day // will convert from Date Object to Number,
    >> // reliably and rapidly.

    >
    >You're not wrong.
    >but isn't automatically converted by the condition (day_user>0) ?


    It would be; but IIRC it would ALSO be converted elsewhere in the code.

    If when assigning to a variable it is the numeric value which matters,
    assign a Number. Unary + will do that.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    For news:borland.*, use their server newsgroups.borland.com ; but first read
    Guidelines <URL:http://www.borland.com/newsgroups/guide.html> ff. with care.
    Dr J R Stockton, Sep 10, 2007
    #9
  10. In comp.lang.javascript message <
    glegroups.com>, Mon, 10 Sep 2007 09:12:54, dlrider <>
    posted:

    >This would work for our purposes, except I would like it to bump the
    >month back one by default. I found how to do that but when the
    >current date is January the month becomes "00". How would I get it to
    >bump the month to "12" and then the year back one?


    When you think you have almost solved that - see below - remember to
    consider what should happen if the initial date is May 31st or March
    29th.

    If permissible, go back 30 days or 4 weeks instead.

    It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <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 J R Stockton, Sep 10, 2007
    #10
  11. dlrider

    ASM Guest

    En réponse à Dr J R Stockton qui écrivit, en date du : 10/09/07 21:41,
    le message suivant :
    > In comp.lang.javascript message <46e48ee1$0$5075$
    >> , Mon, 10 Sep 2007 02:23:11, ASM <

    > valid> posted:
    >> En réponse à Dr J R Stockton qui écrivit, en date du : 8/09/07 22:36,
    >> le message suivant :
    >>> In comp.lang.javascript message <46e1e2e1$0$25941$
    >>> r>, Sat, 8 Sep 2007 01:44:50, ASM <
    >>> valid> posted:


    >>> Date.parse will then convert back (and
    >>> would get it wrong for most of the First Century AD).

    >> I hope there will be no meeting planed at this time :)

    >
    > But it should be considered when coding for the Date of Easter!


    BIPM and IERS with UTC work on (can give) Date of Easter ?

    --
    sm
    ASM, Sep 11, 2007
    #11
  12. In comp.lang.javascript message <46e5d1ca$0$5110$
    >, Tue, 11 Sep 2007 01:20:54, ASM <

    valid> posted:
    >En réponse à Dr J R Stockton qui écrivit, en date du : 10/09/07 21:41,
    >le message suivant :
    >> In comp.lang.javascript message <46e48ee1$0$5075$
    >>> , Mon, 10 Sep 2007 02:23:11, ASM <

    >> valid> posted:
    >>> En réponse à Dr J R Stockton qui écrivit, en date du : 8/09/07 22:36,
    >>> le message suivant :
    >>>> In comp.lang.javascript message <46e1e2e1$0$25941$
    >>>> r>, Sat, 8 Sep 2007 01:44:50, ASM <
    >>>> valid> posted:

    >
    >>>> Date.parse will then convert back (and
    >>>> would get it wrong for most of the First Century AD).
    >>> I hope there will be no meeting planed at this time :)

    >> But it should be considered when coding for the Date of Easter!

    >
    >BIPM and IERS with UTC work on (can give) Date of Easter ?


    That may depend on what Prayer Books they have, and what French
    legislation says; the EU and ISO apparently have not addressed the
    matter (logically, if ISO has not, then EU needs to).

    But the Date of Easter commemorates an event of the First Century AD;
    and in testing aspects of its behaviour it's nice to have an algorithm
    which behaves properly all the way down to the Year Zero. OTOH,
    calculating the Date of Easter does not need a Date Object.


    By the way, my latest Date code (js-misc1.htm, foot) is


    function DayCheck2(El) { var St, RE, A, D, W, X, Y, Z
    St = El.value
    RE = /(\D{0,80}\b)(\d+)-(\d\d)-(\d\d)\s+([a-z]{3})\b/gi
    RE.lastIndex = 0 // Seems needed in FF & Op // B
    while (true) {
    A = RE.exec(St) ; RE.lastIndex -= 10 // max? // C
    if (!A) break
    D = new Date(Date.UTC(+A[2]+400, A[3]-4801, A[4])) // A
    X = (D.getUTCMonth() != A[3]-1) // Y M D invalid
    W = Week[D.getUTCDay()]
    Y = (W != A[5]) && ! A[5].match(/UTC|GMT|and|was/)
    if (X || Y) {
    Z = 'After\n"' + A[1] + '"\n\n' + A[2]+"-"+A[3]+"-"+A[4]
    Z += (X ? " invalid " : Y ? " "+A[5]+" -> " : " ?!?") + W
    if (!confirm(Z+"\n\nOK to proceed or Cancel to quit?")) break
    } } }

    Note :
    (0) Week is an obvious Global array.
    (1) Line A; the 400 & 4800 are to make it work in years 0000 to 0099.
    (2) UTC for speed. FireFox has a known bug in Date.UTC( , ,0) which I
    should attend to here - see (5).
    (3) Line B; a second call of the function fails in FF & Opera without
    that line, but works without it in IE6. Which is right?
    (4) Line C; the -=10 is a bodge so that some of a preceding date can be
    read into A[1] as preceding context without going far enough to find a
    preceding date again.
    (5) Now D = new Date(Date.UTC(+A[2]+400, A[3]-4801, +A[4]+1)-864e5)


    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
    Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
    Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
    Dr J R Stockton, Sep 11, 2007
    #12
    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. Microsoft_Public

    Javascript two fields merged to one hidden field

    Microsoft_Public, Dec 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    575
    Karl Seguin
    Dec 14, 2005
  2. =?Utf-8?B?T3JiaXRhbGU=?=

    How to access input fields written into a label field

    =?Utf-8?B?T3JiaXRhbGU=?=, Mar 31, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    359
    Peter Rilling
    Mar 31, 2006
  3. AMT2K5
    Replies:
    1
    Views:
    181
    Eric Schwartz
    Nov 8, 2005
  4. JohnnyW
    Replies:
    6
    Views:
    131
    Dr John Stockton
    Mar 18, 2006
  5. Replies:
    23
    Views:
    479
    Tim Streater
    Dec 18, 2013
Loading...

Share This Page