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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New to JS, need help with function

    Hello all,

    I am new to JS, so please excuse n00b question.

    I have multiple forms on a page. Each form has a common hidden field, that holds a selected date. Each field is named the same with a number suffix.

    For example,

    seldate1, seldate2, seldate3, etc...

    Each formname is subsequently the same except a suffix.

    While I can check to see if the seldate# field is populated for each individual form using individual functions, I prefer to only have one function. So I made a function that accepts two parameters, one for formname, and one for fieldname:

    function checkemptydate(formname, fieldname)
    {
    if ( document.formname.fieldname.value == '' )
    {
    alert('You have not selected a date to deliver! Please go back and select a date!')
    return false;
    }
    }
    </script>

    So on each form's onClick, I have this for example:

    onClick="javascript:return checkemptydate('teacherslunchbox_1_ATC_0','selDaycal1')"


    Where I am failing is trying to use those parameter names in the line that checks the value (since I guess, I am using parameter names instead of the acual object names, so it cannot find the object in question)

    if ( document.formname.fieldname.value == '' )

    So my question is, how can I use the passed parameters? Or am I doing this the wrong way?

    Thanks in advance to everyone...
    Last edited by Fishwagon; 12-14-2009 at 09:05 PM.

  • #2
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    Try replacing your js function with this:

    Code:
    function checkemptydate(formname, fieldname)
    {
    if ( eval("document."+formname+"."+fieldname+".value") == '' )
    {
    alert('You have not selected a date to deliver! Please go back and select a date!')
    return false;
    }
    }
    Another solution would be passing the object by itself as parameter:


    Code:
    function checkemptydate(myObj)
    {
    if ( myObj.value == '' )
    {
    alert('You have not selected a date to deliver! Please go back and select a date!')
    return false;
    }
    }
    and call it like this:

    Code:
    onClick="checkemptydate(this)"
    Software and cathedrals are much the same - first we build them, then we pray.

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [[[[[hitting self on head]]]]]

    Thanks, makes total sense (now)

    Appreciate the help very much

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,657 Times in 4,619 Posts
    Not to ask a silly question, but...

    Why do you make the field names different in the different forms???

    There's no reason to.

    Code:
    <form name="form1">
    <input type="hidden" name="selDate" />
    ...
    </form>
    ...
    <form name="form27">
    <input type="hidden" name="selDate" />
    ...
    </form>
    There's no conflict. You can always address any single hidden field via
    Code:
    document.form13.selDate
    etc.

    And you could choose the correct form quite easily:
    Code:
    function checkemptydate( formNumber )
    { 
        if ( document.forms["form" + formNumber].selDate.value == '' ) 
       { 
            alert('You have not selected a date to deliver! Please go back and select a date!') 
            return false; 
        } 
        return true; // DO NOT FORGET THIS!!
    }
    Or, of course:
    Code:
    function checkemptydate( form )
    { 
        if ( form.selDate.value == '' ) 
       { 
            alert('You have not selected a date to deliver! Please go back and select a date!') 
            return false; 
        } 
        return true; // DO NOT FORGET THIS!!
    }
    and then
    Code:
    onClick="javascript:return checkemptydate(this.form);"
    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.


  •  

    Posting Permissions

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