Not signed in (Sign In)

Vanilla 1.1.5a is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthornorcomm
    • CommentTimeJan 11th 2010
     
    Trying to grab dates inputted by the user and then running some javascript on it to generate a date schedule and then send it back to them in the auto-responder email.
    1. need to know what date variable is called. (same as what the HTML code says?)
    2. need to know what the date variable is called when the user dynamically adds another date.

    Coding Flow
    user enters in 04/05/2010 for a meeting date.
    if date entered is not at least 6 weeks out from the current date then it forces the user to enter a new date.
    if it is then it generates 5 dates that are 13, 17, 20, 25 and 28 days prior to date entered and sends it in the auto responder.

    I can write most of the JS code, I just need to know the variable names and how to include the generated dates into the auto responder.

    any help is greatly appreciated.
    Thanks.
    • CommentAuthordbuschho
    • CommentTimeJan 11th 2010
     
    Hello,

    You can grab the text of the date entered by the user using the field id, which I think is what you mean by 'same as what the HTML code says').

    for example:
    document.getElementById('tfa_Calendar').value
    will return the text value of the field 'tfa_Calendar'. Which you can then parse through javascript however you like.

    With the generated fields, I'd recommend creating hidden fields to store the dates into, and assigning your calculated values the same way:
    document.getElementById('tfa_HiddenField1').value = calcDate1;
    document.getElementById('tfa_HiddenField2').value = calcDate2;


    Then in the autoresponder you could use those fields (with a pro plan) as:

    Your first next date is: %%tfa_HiddenField1%%
    ...
    etc.


    Hope that helps, happy to answer further questions,
    FA Support.
    • CommentAuthornorcomm
    • CommentTimeJan 11th 2010
     
    Thank you.

    I've setup my javascript and as long as document.getElementById('tfa_Date').value grabs the user's entered date, it should work. However, I've set it up so that an alert comes up. Will this stop the form from submitting or do I need to call a function to stop the form from sending if the alert pops up?
    • CommentAuthornorcomm
    • CommentTimeJan 11th 2010
     
    Basically, I want the form to validate if the user's date entered is at least 6 weeks from the current date.

    this is the code I pasted into the "custom code" section.

    <script language="JavaScript">
    var laterdate = document.GetElementById('tfa_Date').value;// User's date Previous data: new Date(2010, 1, 12)
    var earlierdate = new Date(); // today's date

    timeDifference(laterdate,earlierdate);

    //function timeDifference(laterdate,earlierdate) {
    var difference = laterdate.getTime() - earlierdate.getTime();

    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24

    if (daysDifference <42)
    alert('Your Meeting Date needs to be at least 6 weeks from today.');

    // document.write('difference = ' + daysDifference);
    // document.write (earlierdate);
    //}
    </script>



    It worked fine when I had the function running on my local computer but it's not responding within the form.

    Any help would be great.
    • CommentAuthordbuschho
    • CommentTimeJan 11th 2010
     
    Hello,

    could you point us to the form that has your code on it?
    • CommentAuthornorcomm
    • CommentTimeJan 11th 2010
     
    http://www.tfaforms.com/136176

    I'm trying to apply the script to date field that's generated dynamically when you click on "GROUP (Fill out date, place & time only once below)" under MEETING INFORMATION.

    Thanks.
    • CommentAuthornorcomm
    • CommentTimeJan 12th 2010
     
    Here's a more cleaned up version of my javascript. I took out my comments.

    <script language="JavaScript">

    var laterdate = document.GetElementById('tfa_Date');
    var earlierdate = new Date();

    timeDifference(laterdate,earlierdate);

    var difference = laterdate.getTime() - earlierdate.getTime();

    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24;

    if (daysDifference <42)
    alert('Your Meeting Date needs to be at least 6 weeks from today.');

    </script>
    • CommentAuthordbuschho
    • CommentTimeJan 12th 2010
     
    Hello,

    Off the bat, your call to timeDifference is invalid, as the function hasn't been defined yet, and I think the laterdate.getTime() call is invalid as well, since per:
    var laterdate = document.GetElementById('tfa_Date');
    laterdate is just a html input object.

    Did you mean:
    var laterdate = new Date(document.getElementById('tfa_Date').value);
    ( note that GetElementById is invalid, the function is getElementById )
    ?

    Note that this is apparently configured to run when the page is loaded, so their won't be any values for the code to work with yet.

    If you're working with javascript, it's a very good idea to download firefox and install the plugin firebug, which will be able to tell you where your errors are being generated and allow you to test your code dynamically.
    • CommentAuthornorcomm
    • CommentTimeJan 12th 2010
     
    I guess the next question is how can I call my script to action when the user clicks the submit button when the only place I can insert the script is the in the custom code section?

    I guess my JS is not as great as I initially thought.

    thank you.
    • CommentAuthordbuschho
    • CommentTimeJan 12th 2010 edited
     
    In part, by doing it the way you had before. Once you define a function in the custom code block, you can call that function within the formula section of a input element.

    In this case, you'd probably want to define your function timeDifference() as I've defined below, then call it within the user selected date field. It will be fired by the onChange event.
    ( note I'm going over this pretty fast and not testing my code as you're familiar with javascript. If anyone else is less familar, you should check out:
    http://www.w3schools.com/jS/default.asp
    or any of many many *many* javascript tutorials online )

    so:

    <script>
    function timeDifference() {

    var laterdate = new Date(document.getElementById('tfa_Date').value); // User's date
    var earlierdate = new Date(); // today's date

    if(laterdate && ealierdate){ //Program defensively, make sure laterdate and ealierdate are set

    var difference = laterdate.getTime() - earlierdate.getTime();
    var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24; // Why is this line needed?

    if (daysDifference <42)
    alert('Your Meeting Date needs to be at least 6 weeks from today.');

    }

    };
    </script>

    Like I said, not sure that will run, but give it a try and it should get you closer.
    • CommentAuthornorcomm
    • CommentTimeJan 13th 2010
     
    so I finally got something working but I grabbed the entire code and had to mess with it outside of form assembly because the only way I knew how to call my function was to give the form a name and add an onSubmit.

    now is there any way for me to get this back into formassembly.com so you guys can host it?

    Here's the date validation script in the <head> of my form:

    <script language="JavaScript">

    function dateValidate()
    {
    var today = new Date;
    var userDate = new Date(document.getElementById('tfa_Date').value); // User's date
    //var deadline = today.setDate(today.getDate() + 42);//now + 42 days.

    // The number of milliseconds in one day
    var oneDay = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var userDate_ms = userDate.getTime()
    var today_ms = today.getTime()

    // Calculate the difference in milliseconds
    var difference = Math.abs(userDate_ms - today_ms)

    // Convert back to days and return
    var dayDiff = Math.round(difference/oneDay+1)

    if (dayDiff < 42)
    {
    alert('Your meeting date is not at least 6 weeks out from today.');
    document.CSI_form.tfa_Date.focus();
    return false;
    }
    else
    return true;
    }
    </script>
    • CommentAuthordbuschho
    • CommentTimeJan 13th 2010
     
    Hello,

    In order to get the element, you should only need the document.getElementById().focus No need for a named form. If you are sure you want this running with the onSubmit event, you can add it by this line of code in the custom code block:

    base2.DOM.Element.addEventListener(document.forms[0],"submit",dateValidate);

    But your current code won't stop an invalid submission. What you want is probably something closer to this:
    http://www3.formassembly.com/forum/discussion/454/validating-or-confirming-an-email-address/