Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

How can i validate if the multiselect array is empty?(fallos[]) heres my code :

<tr><td>Fallos reportados: </td><td><select name="fallos[]" size="6" multiple="multiple" >
    <?php 
    $query = "SELECT falla_id,falla FROM falla order by falla";
    $result = mysql_query($query);
    if (!$result) die ("Database access failed: " . mysql_error());
while ($fila = mysql_fetch_array($result, MYSQL_ASSOC)){
    echo "<option value=\"".$fila["falla_id"]."\">".$fila["falla"]."</option>";
}
    ?></td>
</tr>

and here is the validation that i´m making but doesn´t works

function validate(form) {
    fail  = validateIMEI(form.IMEI.value)
    fail += validateTelefono(form.numero_telefono.value)
    fail += validateICCID(form.ICCID.value)
    fail += validateFallos(form.fallos.value)
    if (fail === "") return true
    else { alert(fail); return false }

}

Here is the specific option that validates fallos[]:

function validateFallos(field) {
    if(field=== "") return "No se ha ingresado tipo de fallos"
    //if (field==="") return "No se han ingresado los tipos de fallos.\n"
    return ""
}
share|improve this question

2 Answers 2

up vote 3 down vote accepted
validateFallos(form.fallos.value)

form does not contain fallos. You have select element with name fallos[] that you can access by form['fallos[]'] or more preferred form.elements['fallos[]']. If you want to stick with form.fallos you shoul add id="fallos" to your select tag.

share|improve this answer
1  
+1 for spotting that JavaScript doesn't recognise [] as having special meaning in a name attribute. Following jsfiddle may be of interest jsfiddle.net/Rs3N3 –  Paul S. Jul 17 '13 at 22:39
    
OK it works very fine with form.fallos.value when adding the id=fallos thanks!!!!! –  user2565755 Jul 18 '13 at 0:49

You're missing semicolons at the end of your lines of code...

function validate(form) {
    fail  = validateIMEI(form.IMEI.value)
    fail += validateTelefono(form.numero_telefono.value)
    fail += validateICCID(form.ICCID.value)
    fail += validateFallos(form.fallos.value)
    if (fail === "") return true
    else { alert(fail); return false }
}

Should be

function validate(form) {
    fail  = validateIMEI(form.IMEI.value);
    fail += validateTelefono(form.numero_telefono.value);
    fail += validateICCID(form.ICCID.value);
    fail += validateFallos(form.fallos);
    if (fail === "") return true;
    else { alert(fail); return false; }
}

And also, the multiple select input would contain an array, so you can't just check to see if it's equal to "". Instead, loop through the array and check if any are selected, like so:

function validateFallos(fallosField) {
    var empty = true;
    for (i=0; i < fallosField.length; i++) {
        if (fallosField[i].selected) {
            return "";
        }
    }
    return "No se ha ingresado tipo de fallos";
}

Note how I changed fail += validateFallos(form.fallos.value) to fail += validateFallos(form.fallos).

share|improve this answer
    
This looks like all you did is add semicolons ;, which are optional (but preferred) in this context anyway. –  Paul S. Jul 17 '13 at 22:36
    
Oh wow, how stupid of me - too much PHP! Forgot semicolons aren't compulsory in JS. Anyway, I updated my answer to give you a fix. –  SharkofMirkwood Jul 17 '13 at 22:37
    
This is still wrong :) –  Paul S. Jul 17 '13 at 22:39
    
Only just realised that you aren't the OP. But yeah... I didn't spot that mistake :P –  SharkofMirkwood Jul 17 '13 at 22:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.