SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member Olaf's Avatar
    Join Date
    Dec 2003
    Location
    Netherlands
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with form object

    Hallo,

    I want to use an array of fieldnames for form validation like:

    PHP:

    function CheckForm(theform) {

    req_velden = new Array("bedrijf","contact","adres","plaats","postcode","land","telefoon");

    for (var i = 0; i < req_velden.length; i++) {

    var field = theform.req_velden[i];




    I use the event "onsubmit="CheckForm(this)" in the form-tag to call the function.

    the output with "alert(field)" doesn't work. I get en error in the debugger like:

    Error: theform.req_velden has no properties
    Source File: http://localhost/sparts/genHtml/tes...land=&telefoon=
    Line: 11

    What is wrong here?

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    PHP-ville
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, req_velden doesn't belong to theform, now does it?

    You probably want something like:

    for ( i = 0, max=req_velden.length(); i < max; i++) {
    var veld = eval("theform." + req_velden[i]);

    if (veld) {
    /* etc */
    }
    }

    Please also remember that Javascript validation of forms is not enough to provide security. You will have to run the same validations from your server side handler aswell!

    Edit: Concatentation is done with + in Javascript, not with the dot like in PHP.

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Avoid using eval() at all costs. Instead, for this instance, use the elements collection
    Code:
    var veld = theform.elements[req_velden[i]];
    Or, you can use a javascript validation library, like mine
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

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