Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please change your validation and form to this which will allow submission of the form if valid and not if errors. The following code is in my opinion <em>canonical</em> and will work on all browsers that support regular expressions (which was introduced in JS1.1 in 1996 with NS3.0) - please note that javascript does not support dashes in names unless you quote the field name in the script. The code does not need the form to be named since it passes the form object in the call <em>(this)</em> and uses the object in the function as <em>theForm</em></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Canonical forms validation without jQuery&lt;/title&gt; &lt;script type="text/javascript"&gt; var validName = /^[A-Za-z]+$/; function checkName(str) { return validName.test(str); } function verify(theForm) { // note: theForm["..."] is short for theForm.elements["..."]; var amount = theForm["exp_amount"].value; if(amount ==""){ alert("Block Amount is left blank"); theForm["exp_amount"].focus(); return false; } if (isNaN(amount)) { alert("Invalid Block Amount"); theForm["exp_amount"].focus(); return false; } var name = theForm["exp_name"].value; if(name.length==0) { alert("Block Exp is left Blank!"); theForm["exp_name"].focus(); return false; } if(!checkName(name)) { alert("Block Exp is invalid!"); theForm["exp_name"].focus(); return false; } return true; } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form onsubmit="return verify(this)"&gt; Amount: &lt;input type="text" name="exp_amount" value="" /&gt;&lt;br /&gt; Name: &lt;input type="text" name="exp_name" value="" /&gt;&lt;br /&gt; &lt;input type="submit" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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