Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It sounds like you just wanted to verify if everything inside the form element was validated. Here are two fiddles I worked up to get you started:</p> <p>1) Regular Javascript</p> <pre><code>function formCheck() { var form = document.getElementById('mainForm'), inputs = form.getElementsByTagName('INPUT'), selects = form.getElementsByTagName('SELECT'), i, errors = []; for (i = 0; i &lt; inputs.length; i++) { if (inputs[i].value.match(/^\s*$/)) { errors.push(inputs[i].title + ' was not set'); } } for (i = 0; i &lt; selects.length; i++) { if (selects[i].selectedIndex === 0) { errors.push(selects[i].title + ' was not set'); } } if (errors.length &gt; 0 ) { alert(errors.join('\n')); return false; } alert('success!'); return true; } </code></pre> <p><a href="http://jsfiddle.net/fstreamz/3Decx/4/" rel="nofollow">http://jsfiddle.net/fstreamz/3Decx/4/</a></p> <p>2) With JQuery</p> <pre><code>function formCheck() { var formElements = $('#mainForm').find('input, select'), errors = []; $.each(formElements, function(i, el) { if ($(el).val().match(/^\s*$/)) { errors.push($(el).prop('title') + ' is blank!'); } }); if (errors.length &gt; 0 ) { alert(errors.join('\n')); return false; } alert('success'); return true; } </code></pre> <p><a href="http://jsfiddle.net/fstreamz/w5KG3/4/" rel="nofollow">http://jsfiddle.net/fstreamz/w5KG3/4/</a></p> <p>A few notes: 1) It checks every input or select element inside a form element with id="mainForm" so you'd have to add that to your form (next to name="myForm"). 2) I added a title attribute to the input/select elements so I could show that in the error message as a reference. 3) It checks for whitespace in input boxes and consider that invalid. 4) in the jsFiddle, it returns false even in success so the form doesn't submit for demonstration purposes. That should be changed to true if you use it.</p>
    singulars
    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