Note that there are some explanatory texts on larger screens.

plurals
  1. PO.focus() not working in Javascript Function
    primarykey
    data
    text
    <p>I have simple form with fields for First and Last name and a salutation. I have a javascript function that should validate if the fields or empty or exceed 25 characters. The function mostly works except on .focus(); the page just reloads. Not sure what I'm doing wrong, please advise. Thanks in advance.</p> <pre><code>function validate(){ //alert ("TEST!!!"); var first = document.getElementById('firstname').value; if (first.length == 0 || first.length &gt; 25){ alert ("Please enter your first name, no longer than 25 chracters."); document.getElementById('firstname').focus(); return false; } var last = document.getElementById('lastname').value; if (last.length == 0 || last.length &gt; 25){ alert ("Please enter your last name, no longer than 25 characters."); document.getElementsByName('lastname').focus(); return false; } var title = document.getElementById('title').value; if (document.getElementById('title').selectedIndex == 0){ alert ("Please select your salutation"); document.getElementById('title').focus(); return false; } return true; </code></pre> <p>}</p> <p>In the html the form is: </p> <pre><code>&lt;form name="name" form id="name" method="post" onsubmit="validate();"&gt; Salutation: &lt;select name="title" select id="title"&gt; &lt;option selected="Please Select"&gt;Please select&lt;/option&gt; &lt;option value="Mr."&gt;Mr.&lt;/option&gt; &lt;option value="Mrs."&gt;Mrs.&lt;/option&gt; &lt;option value="Miss"&gt;Miss&lt;/option&gt; &lt;/select&gt;&lt;br&gt;&lt;br&gt; First Name : &lt;input type="text" input id="firstname" name="firstname"&gt; Last Name : &lt;input type="text" input id="lastname" name="lastname"&gt;&lt;br&gt;&lt;br&gt; &lt;input type="submit" value="Submit"&gt;&lt;br&gt;&lt;br&gt; &lt;/form&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.
 

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