Sunday, 15 April 2012

Javascript + HTML How to validate multidimensional select objects -



Javascript + HTML How to validate multidimensional select objects -

i trying validate material dropdowns. getting frm.material undefined error on system.

html markup

<form name="testfrm" id="textfrm">material 1 <select name="material[1][]" class="form_dropdown material_dd"> <option value="-1">select</option> <option value="2">fine silk</option> <option value="10">nz wool</option> <option value="3">thick silk</option> <option value="1">titbetan wool</option> </select> <select name="material[1][]" class="form_dropdown material_dd"> <option value="-1">select</option> <option value="2">fine silk</option> <option value="10">nz wool</option> <option value="3">thick silk</option> <option value="1">titbetan wool</option> </select> <br />material 2 <select name="material[2][]" class="form_dropdown material_dd"> <option value="-1">select</option> <option value="2">fine silk</option> <option value="10">nz wool</option> <option value="3">thick silk</option> <option value="1">titbetan wool</option> </select> <br />material 3 <select name="material[3][]" class="form_dropdown material_dd"> <option value="-1">select</option> <option value="2">fine silk</option> <option value="10">nz wool</option> <option value="3">thick silk</option> <option value="1">titbetan wool</option> </select> <select name="material[3][]" class="form_dropdown material_dd"> <option value="-1">select</option> <option value="2">fine silk</option> <option value="10">nz wool</option> <option value="3">thick silk</option> <option value="1">titbetan wool</option> </select> <select name="material[3][]" class="form_dropdown material_dd"> <option value="-1">select</option> <option value="2">fine silk</option> <option value="10">nz wool</option> <option value="3">thick silk</option> <option value="1">titbetan wool</option> </select> <br /> <input type="button" onclick="return checkform()" value="validate" /> </form>

i have created jsfiddle can find here http://jsfiddle.net/qgghz/12/

the value of first select alternative <option value="-1">select</option> , need check if of dropdown not set select.

i need js function validate these dropdowns.

thank back upwards in advance

edit

thank fixing jsfiddle issues, need validation function can iterate on these dropdowns.

edit

if utilize next js shows names

var boxes = document.getelementsbytagname('select'); for(i = 0; < boxes.length; i++){ console.log(boxes[i].name); } // output: material[1][], material[1][], material[2][] .... someotherdropdowns

answer

here how got values

function checkform(){ var e = false; var boxes = document.getelementsbyclassname('material_dd'); for(i = 0; < boxes.length; i++){ if(boxes[i].value == '-1'){ e = true; } } if(e){ alert('please select material'); homecoming false; }

}

so check material dropdowns if there other select boxes well. , can iterate on value see if have -1 in that.

besides typo on form name, not array of selects through frm.material, not grouped this, when receive form in backend script. access them frm["material[1][]"], instead of i'd utilize aproach document.getelementsbyclassname('material_dd') can know how many elements there.

javascript html multidimensional-array

No comments:

Post a Comment