Validating form arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lüpher Cypher

    #1

    Validating form arrays

    Hi,
    I have a form:

    <form .. onSubmit="retur n onFormSubmit(th is)">
    <input type="checkbox" name="a[]" value="1" />
    <input type="checkbox" name="a[]" value="2" />
    ...
    <input type="checkbox" name="a[]" value="n" />
    </form>

    I'm trying to verify whether any (i.e. >=1) of the fields are selected:

    if (f.a.length < 1) {
    // fail
    }

    But it doesn't seem to work.. I figure a's gotta be an array (it is in
    php and I'm usually working in php and avoid js, but this is something
    I'd rather do in js)

    Thanks
    Lüph
  • RobB

    #2
    Re: Validating form arrays

    Lüpher Cypher wrote:[color=blue]
    > Hi,
    > I have a form:
    >
    > <form .. onSubmit="retur n onFormSubmit(th is)">
    > <input type="checkbox" name="a[]" value="1" />
    > <input type="checkbox" name="a[]" value="2" />
    > ..
    > <input type="checkbox" name="a[]" value="n" />
    > </form>
    >
    > I'm trying to verify whether any (i.e. >=1) of the fields are[/color]
    selected:[color=blue]
    >
    > if (f.a.length < 1) {
    > // fail
    > }
    >
    > But it doesn't seem to work.. I figure a's gotta be an array (it is[/color]
    in[color=blue]
    > php and I'm usually working in php and avoid js, but this is[/color]
    something[color=blue]
    > I'd rather do in js)
    >
    > Thanks
    > Lüph[/color]

    In php: it's an array !
    In JavaScript: it's a....name.

    if (refToForm.elem ents['a[]'].length < 1) {

    The .elements collection reference is optional. Treating the square
    brackets purely as string characters isn't.

    And of course, the obligatory link:



    Comment

    • Michael Winter

      #3
      Re: Validating form arrays

      Lüpher Cypher wrote:

      [snip]
      [color=blue]
      > I'm trying to verify whether any (i.e. >=1) of the fields are selected:
      >
      > if (f.a.length < 1) {
      > // fail
      > }
      >
      > But it doesn't seem to work..[/color]

      In PHP (or any server-side language) it would have the expected result
      because you receive only the submitted data. However, client-side
      ECMAScript will see all of the data.
      [color=blue]
      > I figure a's gotta be an array[/color]

      It isn't, but 'a[]' is (well, a collection to be more accurate).
      [color=blue]
      > (it is in php [...][/color]

      That's just how PHP requires you to handles multiple values under the
      same name. Whilst ECMAScript will create a collection of form
      controls, it will be based on the entire name as it appears in the
      attribute.
      [color=blue]
      > [...] this is something I'd rather do in js)[/color]

      You should be doing it in both. Client-side for speed and convenience.
      Server-side for reliability.

      /* g - reference to a form control group */
      function hasChecked(g) {
      for(var i = 0, n = g.length; i < n; ++i) {
      if(g[i].checked) {return true;}
      }
      return false;
      }

      function onFormSubmit(f) {var e = form.elements;
      if(!hasChecked( e['a[]'])) {
      /* No checked elements. */
      }
      }

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      Working...