SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Threaded View

  1. #1
    SitePoint Enthusiast subBlue's Avatar
    Join Date
    Jun 2001
    Edinburgh, UK
    0 Post(s)
    0 Thread(s)

    Javascript validation of selector with multiple select enabled?

    I have a select box in a form which enables multiple item selection. I would like to use javascript to check that at least one item has been selected before the form is sent off.

    I have my validation script working for normal text fields, just this one is a problem.

    To add to the difficulty, i will be processing the results in PHP, which requires funny naming of multiple selection fields by having '[]' after the items name, e.g. members[]

    PHP Code:
    <form name="tester" method="post" action="test_scrap.php"  onSubmit="return validate(this)">
    select name="members[]" size="4" multiple style="width:150px">
    option value="4">Gregor</option>
    option value="3">James</option>
    option value="1">Tom</option>
    input type="submit" name="submit" value="Send it">
    And my validation script looks like (only relavent bits shown):

    PHP Code:
    function validate(formObj) {

    errors = new Array;
    ERROR_PREFIX "Form incomplete!\n\n";
        if (
    formObj.members[].length 1)    
    errors.push("You must pick the task members.");
        if (!    
    errors.push("You must enter the task name.");
        if (
    errors.length 0) {
    alert(ERROR_PREFIX errors.join("\n"));
        } else {

    For normal input fields it's working fine, but for the multiple selection box it doesn't work I realise that having the selection box called 'members[]' is a problem when checking it with javascript, but it's neccessary to have the '[]' for PHP to interpret it as an array.

    Any ideas?

    Last edited by subBlue; Jan 25, 2002 at 08:32.
    Become Interactive because its all gone a bit subBlue


Posting Permissions

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