Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Form validation is the process of checking if the form fields are filled in correct formats if they are processes. In your question, the correct format is "more than 3 characters". Of course, if statements are involved during form validations. In the client-side, you use JavaScript code to validate form fields.</p> <p>Here is how it works: A form has an <code>onsubmit</code> event handler, when the user clicks Submit, or presses Enter, <code>onsubmit</code> will be triggered. <code>onsubmit</code> is where you put the form validation code.</p> <pre><code>&lt;script&gt; function onsubmit() { if (document.forms.mainForm.inputQTY.value.length &lt; 3) { alert("ERROR: Must be more than 3 characters"); return false; // stops the form submission } return true; } &lt;/script&gt; </code></pre> <p>And here is how you add the onsubmit handler</p> ... <p>The <code>onsubmit</code> function can return a value: true to let the form to be submitted, or false to stop the submission.</p> <p>What is document.forms.mainForm.inputQTY.value.length? <code>document.forms.mainForm</code> references to the form <code>name</code>d <code>mainForm</code>, while <code>.inputQTY</code> finds the <code>inputQTY</code> field. You can use <code>document.getElementsByTagName</code> to manually find these elements, but it is [time and space]-consuming to write that type of code.</p> <p>Because some users have JavaScript disabled, or crackers intentionally disable JS to bypass validation, you must validate server-side.</p> <p>Server-side code will be something like this:</p> <pre><code>if (strlen($_GET['inputQTY']) &lt; 3) { echo "ERROR: Must be more than 3 characters"; } else { // submit data } </code></pre> <p>NOTE: code written from head, not tested</p>
 

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