SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check if form field exists and if it's an array

    Hi,

    I want to validate a form. In the form is a text field named 'extra[]' where the user inputs a number. The number is what I want to validate.

    However, the 'extra[]' field is dynamic and does not always appear in the form.

    If it appears, there may be only 1 occurrence of it which means it's then not an array but there may also be multiple occurrences of it.

    I have a script that validates (and work as it should) if the 'extra[]' field occurs multiple times and is thereby an array, but I can't figure out how to validate the 'extra[]' if there is only 1 and to not throw an error if it's not there at all.

    Can anyone help?

    Here's the script that validates if it's an array.

    Code:
    function validateForm(form) {
      
        for (i=0; i<form.elements['extra[]'].length; i++) {
            if (form.elements['extra[]'][i].value > 0) {
                if (form.elements['extra[]'][i].value > 10) {
                    alert("The number can't be more than 10.");
                    return false;
                }
            }
    
        }
        
    }

  2. #2
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you name and id your form element with brackets then it should automatically be created as an array. All you need to do now is check if it exists:
    Code:
    function validateForm(form)
    {
        var errors = "";
    
        for (var i=0; i<form.elements.length; i++)
        {
             //[...]
             if (form.elements[i].name == "extra[]")
             {
                 if (form.elements['extra[]'][i].value > 10) {errors += "The number can't be more than 10.\n");}
                 //[...]
             }
             //[...]
        }
        if (errors.length > 0)
        {
            alert(errors);
            return false;
        }
        else {return true;}
    }
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your reply, but javascript doesn't recognize an array if there's only one element in it (or so it seems). I tried your modified code, but it didn't work. However, I managed to solve it myself and here is the resulting code in case it can help someone else:

    Code:
    function isArray( variable )
    {
        return Boolean( typeof variable == 'object' && variable.length >= 0 );
    }
    
    
    if (isArray(form.elements['extra[]'])) {
        
        for (i=0; i<form.elements['extra[]'].length; i++) {
            if (form.elements['extra[]'][i].value > 0) {
                if (form.elements['extra[]'][i].value > 10) {
                    alert("The number can't be more than 10.");
                    return false;
                }
            }
    
        }
        
    } else {
        
        if(form.elements['extra[]'].value > 10) {
            alert("The number can't be more than 10.");
            return false;
        }
        
    }

  4. #4
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah! Good job in solving it! I did see the mistake in my code. If you are still interested, the following should work:
    Code:
    function validateForm(form)
    {
        var errors = "";
    
        for (var i=0; i<form.elements.length; i++)
        {
             //[...]
             if (form.elements[i].name == "extra[]")
             {
                 if (form.elements[i].value > 10) {errors += "The number can't be more than 10.\n");}
                 //[...]
             }
             //[...]
        }
        if (errors.length > 0)
        {
            alert(errors);
            return false;
        }
        else {return true;}
    }
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.


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
  •