Results 1 to 2 of 2
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    austin, Texas, USA
    Thanked 0 Times in 0 Posts

    javascript hell passing form values to a function

    sigh... javascript is SO irritating. if you don't do everything correctly the script dies completely -- tedious to debug, and especially if you don't really know what you're doing...

    Anyway, here's my situation:
    I am building out validation code for a series of webforms and want to make the code more modular. Basically, create some standard validation and pass the form field of interest to the function. How do I do this? I have some samples but I can't seem to make the magic happen.

    here's a real simple, basic shot at a function to pass a form field value to a function:

    <font face="Georgia">
    function ValidPhone(formField,fieldLabel)
    var elems = form.formField.value

    alert (elems)


    function validateForm(form)

    ValidPhone(form.Work_Phone, "Work Phone")

    </font face=georgia>

    fired by this:
    <font face="Georgia">
    <form method="POST" action="list_elements.asp" form name = "Complainant_Info" onsubmit="return validateForm(this)">
    </font face=georgia>

    As you can see, the magic is supposed to be in passing to the function whatever form field you specify in the array on calling the function. I added the label because I want to refer to the control in an alert box rather than use the form field name. OTT this is really plain vanilla, but, as I stated above, javascript doesn't tell you why it doesn't work, it just dies, silently and quickly.


    <img src=/S/bagged.gif border=0 alt=bagged width=22 height=22>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Silicon Valley, USA
    Thanked 94 Times in 90 Posts

    Re: javascript hell passing form values to a function

    Internet Explorer has a flexible object model that makes it easy to confuse object references and strings. In your example, you pass "this" -- which I imagine to be an object reference to the form, and then next you pass "form.Work_Phone" which might be an object reference to a field or a string, how would you know, and then you insert that into form.form.Work_Phone.value in a second function. Somewhere along the way, this just isn't turning out to be valid. It could be that the second function simply needs the initial form removed, but I haven't built a page to try it.

    If you turn on error reporting in the browser, you can at least get a reference to the problem line. (I think the De###### is not useful, but the error reporting can be.)
    Attached Images Attached Images

Posting Permissions

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