Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write Validation code based on fieldsets that are currently visible using Jquery
    primarykey
    data
    text
    <p>We need to retrieve all the required fields in this codeIgniter/PHP app, since the hide/display js code is used, how can I, using jquery, get all the required fields that are visible? see attached sample html.</p> <p>In the prod app, all the required fields have this css on the label [ * ]. is there a way to pull all these fields using jquery? Here is the catch, issue, this [ * ] is on the label, I actually need get/check that the associate field is empty and the field could be a text, dropdown, checbox, radio, etc. if empty, I need prevent submission of the form and display error message.</p> <p>In short, <strong>OnClick</strong> of the submit button, grab <strong>all fields that are required</strong> that are <strong>in fieldsets that are currently visible</strong> [has to do with detecting height and width] if any of these fields are empty, stop form submission and display error message, ie: "this field is required" "this field is required" "this field is required" [This will go into a div at the top of the page]</p> <p><strong>test code below:</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;meta content="text/html; charset=utf-8" http-equiv="Content-Type" /&gt; &lt;title&gt;Project Overview&lt;/title&gt; &lt;script type="text/javascript"&gt; var projectType_Field_Id = '1169'; //Project Types var projectTypes = new Array ( {id : '11', value:'Cars'}, {id : '12', value:'Boats'}, {id : '13', value:'Planes'}); //occurs onLoad function formOnLoad() { var project_type = document.getElementById(projectType_Field_Id).value; refreshSections(project_type); } /* * Onchange - Project Type */ function projectTypeChange() { var project_type = document.getElementById(projectType_Field_Id).value; refreshSections(project_type); } function refreshSections(selectedType) { for (var i = 0; i &lt; projectTypes.length; i++) { if (projectTypes[i].value == selectedType) { document.getElementById("section-"+ projectTypes[i].id).style.display = ''; } else { document.getElementById("section-"+ projectTypes[i].id).style.display = 'none'; } } } &lt;/script&gt; &lt;/head&gt; &lt;body onload="formOnLoad();"&gt; &lt;form method="post" action=""&gt; &lt;fieldset name="mainSection"&gt; &lt;legend style="color:blue; font-weight:bold"&gt;Project Overview Section&lt;/legend&gt; &lt;table style="width: 100%"&gt; &lt;tr&gt; &lt;td style="height: 33px; width: 178px; color:maroon"&gt;Name&lt;span class="required"&gt; * &lt;/span&gt;&lt;/td&gt; &lt;td style="height: 33px"&gt;&lt;input id="1125" name="1125" type="text" class="valuetext fld_required"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="height: 33px; width: 178px; color:maroon"&gt;Email&lt;span class="required"&gt; * &lt;/span&gt;&lt;/td&gt; &lt;td style="height: 33px"&gt;&lt;input id="1026" name="1026" type="text" class="valuetext fld_required"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="width: 178px"&gt;Product Title&lt;/td&gt; &lt;td&gt;&lt;input id="1089" name="1089" type="text" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="width: 178px"&gt;Product Type&lt;/td&gt; &lt;td&gt;&lt;select id="1169" name="1169" onChange="projectTypeChange();"&gt; &lt;option value=""&gt;Select&lt;/option&gt; &lt;option value="Cars"&gt;Cars&lt;/option&gt; &lt;option value="Boats"&gt;Boats&lt;/option&gt; &lt;option value="Planes"&gt;Planes&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt;&lt;br /&gt;&lt;br /&gt; &lt;fieldset id="section-11" name="section-11"&gt; &lt;legend style="color:fuchsia; font-weight:bold"&gt;Car Details Section&lt;/legend&gt; &lt;table cellpadding="2" style="width: 100%"&gt; &lt;tr&gt; &lt;td style="width: 334px; height: 35px"&gt;&lt;label style="color:maroon"&gt;Size:&lt;/label&gt;&lt;span class="required"&gt; * &lt;/span&gt;&lt;/td&gt; &lt;td style="height: 35px"&gt;&lt;input id="1245" name="1245" type="text" class="valuetext fld_required"/&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="height: 35px; width: 334px"&gt;Color:&lt;/td&gt; &lt;td style="height: 35px"&gt; &lt;select id="1433" name="1433"&gt; &lt;option value="Orange"&gt;Orange&lt;/option&gt; &lt;option value="Blank"&gt;Blank&lt;/option&gt; &lt;option value="Green"&gt;Green&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="width: 334px"&gt;Description:&lt;/td&gt; &lt;td&gt; &lt;textarea id="1290" name="1290" rows="2" style="width: 433px"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt; &lt;br /&gt; &lt;fieldset id="section-12" name="section-12"&gt; &lt;legend style="color:fuchsia; font-weight:bold"&gt;Plane Details Section&lt;/legend&gt; &lt;table cellpadding="2" style="width: 100%"&gt; &lt;tr&gt; &lt;td style="width: 334px; height: 35px"&gt;&lt;label style="color:maroon"&gt;Size:&lt;/label&gt;&lt;span class="required"&gt; * &lt;/span&gt;&lt;/td&gt; &lt;td style="height: 35px"&gt;&lt;input id="1254" name="1254" type="radio" class="valuetext fld_required"/&gt;Small &lt;input id="1254" name="1254" type="radio" /&gt;Large&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="height: 35px; width: 334px"&gt;Color:&lt;/td&gt; &lt;td style="height: 35px"&gt; &lt;select id="1433" name="1433"&gt; &lt;option value="Orange"&gt;Orange&lt;/option&gt; &lt;option value="Blank"&gt;Blank&lt;/option&gt; &lt;option value="Green"&gt;Green&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="width: 334px"&gt;Description:&lt;/td&gt; &lt;td&gt; &lt;textarea id="1290" name="1290" rows="2" style="width: 433px"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt;&lt;br /&gt; &lt;fieldset id="section-13" name="section-13"&gt; &lt;legend style="color:fuchsia; font-weight:bold"&gt;Boat Details Section&lt;/legend&gt; &lt;table cellpadding="2" style="width: 100%"&gt; &lt;tr&gt; &lt;td style="width: 334px; height: 35px"&gt;&lt;label style="color:maroon"&gt;Size:&lt;/label&gt;&lt;span class="required"&gt; * &lt;/span&gt;&lt;/td&gt; &lt;td style="height: 35px"&gt;&lt;select id="1245[]" name="1245[]" class="valuetext fld_required"&gt; &lt;option value=""&gt;Select&lt;/option&gt; &lt;option value="Small"&gt;Small&lt;/option&gt; &lt;option value="Medium"&gt;Medium&lt;/option&gt; &lt;option value="Large"&gt;Large&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="height: 35px; width: 334px"&gt;Color:&lt;/td&gt; &lt;td style="height: 35px"&gt; &lt;select id="1433" name="1433"&gt; &lt;option value="Orange"&gt;Orange&lt;/option&gt; &lt;option value="Blank"&gt;Blank&lt;/option&gt; &lt;option value="Green"&gt;Green&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="width: 334px"&gt;Description:&lt;/td&gt; &lt;td&gt; &lt;textarea id="1290" name="1290" rows="2" style="width: 433px"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt; &lt;fieldset id="section-1011" name="section-1011"&gt; &lt;legend style="color:green; font-weight:bold"&gt;Misc Info Section&lt;/legend&gt; &lt;table cellpadding="2" style="width: 100%"&gt; &lt;tr&gt; &lt;td style="width: 334px; height: 35px"&gt;&lt;label&gt;Size:&lt;/label&gt;&lt;/td&gt; &lt;td style="height: 35px"&gt;&lt;input id="1301" name="1301" type="text" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="height: 35px; width: 334px"&gt;Color:&lt;/td&gt; &lt;td style="height: 35px"&gt; &lt;select id="1302" name="1302"&gt; &lt;option value="Orange"&gt;Orange&lt;/option&gt; &lt;option value="Blank"&gt;Blank&lt;/option&gt; &lt;option value="Green"&gt;Green&lt;/option&gt; &lt;/select&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td style="width: 334px"&gt;Description:&lt;/td&gt; &lt;td&gt; &lt;textarea id="1303" name="1303" rows="2" style="width: 433px"&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/fieldset&gt; &lt;input name="Send Job" type="submit" value="Send Job" /&gt; &lt;/form&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload