Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question show one year from selected date in next text box

    I have two textboxes in a form. The first textbox contains a calender. I would like to display after one year date in second text box based on the calender date selected in first text box. How to achieve this? Date format is MM-DD-yyyy.


    PHP Code:
    <td><input type="text" name="soname3" id="id1" size="15"
     <
    img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('id1','MMddyyyy','dropdown',false,'12')" /></td>

    <
    td><input type="text" name="" id="id2" size="20"> </td

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    And what if someone IGNORES the calendar and just types a date into the first text box? Seems to me you have to handle both cases.

    Do you really want the date in MMddyyyy form, with no delimiters??? That is "07112013" and not "07/11/2013"??
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    I will make the textbox readonly so that users will be able to enter date only using calender. The format will be MM-dd-yyyy

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    Okay, it would still probably be best to modify the code in that NewCssCal( ) function, but assuming that it's obscure and hard to modify code, let's hack it instead.

    Quick and dirty:
    Code:
    <input type="text" name="soname3" id="id1" size="15" readonly />  
    <img src="../Image/cal.gif" id="" style="cursor: pointer;" 
         onclick="NewCssCal('id1','MMddyyyy','dropdown',false,'12'); fixup();" />
    
    <input type="text" name="" id="id2" size="20">
    
    <script type="text/javascript">
    function fixup( )
    {
        var parts = document.getElementById("id1").value.split("/");
        parts[2] = Number(parts[2]) + 1;
        document.getElementById("id2").value = parts.joint("/");
    }
    </script>
    The use of "javascript:" in event handlers (as in your onclick="javascript: ..." is way obsolescent. Kill it.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    I replaced parts.joint with parts.join. Then it started showing results but not what I wanted. Once I select the date from calender as 12-07-2013 then I got "//NaN" as result in other box. But I am expecting 11-07-2014 in second box and not //NaN.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,252
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Replace the "/" in both the split and the join by "-" which is your format.

    Quizmaster: In 2010, of which European country was Michael D. Higgins elected President?
    Contestant: I don't think it was France, and it definitely isn't Germany. I'll go for Spain.


    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Regular Coder
    Join Date
    Aug 2011
    Posts
    101
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question

    It is the same. --NaN now instead of //NaN. When I click on the calender icon for first time, then "--NaN" is sent to second textbox. If I click again on the calender icon for second time, then the current date in box one is sent to box two but not the box one date + one year.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,202
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    Well, clearly I can't test with your calendar control, since I don't have it, but if I just use onchange on a text field it works:
    Code:
    <input type="text" name="soname3" id="id1" size="15" onchange="fixup()" />  
    <input type="text" name="" id="id2" size="20">
    
    <script type="text/javascript">
    function fixup( )
    {
        var parts = document.getElementById("id1").value.split("-");
        parts[2] = Number(parts[2]) + 1;
        document.getElementById("id2").value = parts.join("-");
    }
    </script>
    If I enter 4-13-2013 into the first text field, then 4-13-2014 appears in the second one.

    So there is something you aren't telling/showing us.

    Can you show it in a live URL?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    naveendk.55 (07-14-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •