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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2004
    Location
    New Zealand
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trouble using an argument in a validation function

    Hello,

    I am trying to create a simple form validation check where the name of the form is passed as an argument to a validation function using an onclick event.

    The function is placed on a page which creates several dynamic forms with dynamic names.

    function checkuserprice(formname) {
    if (eval(document.formname.extravagant.value == "")) {
    window.alert("Please insert a price for the extravagant option.");
    document.formname.extravagant.focus();
    return false;}
    return true;
    }

    The function returns a "null or not an object" error. The problem is that "document.formname.extravagant.value" is not being evaluated and I was wondering why not?

    I have hard coded a form name into the function and it has worked as it should

    Cheers for any help.

    Andrew

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Several things that I would correct. First things first, there's no need for the eval part in that code. You're not doing anything you could not do more effectively in other ways. As you might see if you perform a search for eval in this forum, you'll find a lot of discussions about why it's evil.

    The second problem you have is your referencing. formname is an argument variable, but document.formname.extravagant.value is an identifier. That means that you are trying to access a form with the name 'formname' instead of the string that is contained by the variable formname. That problem is easily solved by the usage of document[formname].extravagant.value instead, or even better, document.forms[formname].elements['extravagant'].value.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    New to the CF scene
    Join Date
    Jan 2004
    Location
    New Zealand
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Liorean,

    Your comments corrected my mistakes and the function is working.

    Will look more into argument variables and identifiers.

    Cheers for your help.

    Andrew


  •  

    Posting Permissions

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