Note that there are some explanatory texts on larger screens.

plurals
  1. POinline javascript form validation
    primarykey
    data
    text
    <p>I'm working on a form validation script at work and am having some difficulty. The form is meant to make sure that the user fills out a name, a real-looking email, a category (fromt he drop down) and a question:</p> <p>This names the form and gathers all the data up from the form:</p> <pre><code>&lt;script&gt; function checkForm(form1) { name = document.getElementById("FieldData0").value; category = document.getElementById("FieldData3").value; question = document.getElementById("FieldData1").value; email = document.getElementById("FieldData2").value; </code></pre> <p>This checks to see that something is in the "name" field. It works fine and validates exactly like it should, displaying the error text:</p> <pre><code> if (name == "") { hideAllErrors(); document.getElementById("nameError").style.display = "inline"; document.getElementById("FieldData0").select(); document.getElementById("FieldData0").focus(); return false; </code></pre> <p>This also works just like it should. It checks to see if the email field is empty and if it is empty,displays error text and selects that field:</p> <pre><code> } else if (email == "") { hideAllErrors(); document.getElementById("emailError").style.display = "inline"; document.getElementById("FieldData2").select(); document.getElementById("FieldData2").focus(); return false; } </code></pre> <p>This also works just like it should, makes sure that the questions field isn't empty:</p> <pre><code>else if (question == "") { hideAllErrors(); document.getElementById("questionError").style.display = "inline"; document.getElementById("FieldData1").select(); document.getElementById("FieldData1").focus(); return false; } </code></pre> <p>This one works partially - If no drop down is selected, it will display the error message, but that doesn't stop the form from submitting, it just displays the error text while the form submits:</p> <pre><code>else if (category == "") { hideAllErrors(); document.getElementById("categoryError").style.display = "inline"; document.getElementById("FieldData3").select(); document.getElementById("FieldData3").focus(); return false; } </code></pre> <p>This one doesn't work at all no matter where I put it. I used a variation on the same script last week and it worked fine. This is supposed to check to see that the email entered looks like a real email address:</p> <pre><code>else if (!check_email(document.getElementById("FieldData1").value)) { hideAllErrors(); document.getElementById("emailError2").style.display = "inline"; document.getElementById("FieldData2").select(); document.getElementById("FieldData2").focus(); return false; } Otherwise it lets the form submit: return true; } </code></pre> <p>This checks the email out:</p> <pre><code>function check_email(e) { ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM"; for(i=0; i &lt; e.length ;i++){ if(ok.indexOf(e.charAt(i))&lt;0){ return (false); } } if (document.images) { re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/; re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; if (!e.match(re) &amp;&amp; e.match(re_two)) { return (-1); } } } </code></pre> <p>This function hides all errors so the user isn't bombarded with red text. I tried putting in "document.getElementById("emailError").style.display = "none"" but that breaks the whole thing:</p> <pre><code> function hideAllErrors() { document.getElementById("nameError").style.display = "none" document.getElementById("emailError").style.display = "none" document.getElementById("categoryError").style.display = "none" document.getElementById("questionError").style.display = "none" } &lt;/script&gt; </code></pre> <p>And the form looks like this:</p> <pre><code>&lt;form onSubmit="return checkForm();" method="post" action="http://www.emailmeform.com/fid.php?formid=303341io4u" name="form1"&gt; &lt;p&gt;&lt;div class=error id=nameError&gt;Required: Please enter your name&lt;br/&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Name:&lt;/strong&gt; &lt;span&gt;&lt;/span&gt;&lt;br&gt;&lt;input type="text" name="FieldData0" id="FieldData0" value="" size="22" tabindex="1" /&gt; &lt;label for="name"&gt;&lt;/label&gt;&lt;/p&gt; &lt;p&gt;&lt;div class=error id=emailError&gt;Required: Please enter your email address&lt;br/&gt;&lt;/div&gt; &lt;div class=error id=nameError2&gt;This doesn't look like a real email address, please check and reenter&lt;br/&gt;&lt;/div&gt; &lt;strong&gt;&lt;p&gt;Email:&lt;/strong&gt; &lt;span&gt;(will not be published)&lt;/span&gt;&lt;br&gt;&lt;input type="text" name="FieldData2" id="FieldData2" value="" size="22" tabindex="2" /&gt; &lt;label for="email"&gt;&lt;/label&gt; &lt;/p&gt; &lt;div class=error id=categoryError&gt;Please select a category from the drop-down menu&lt;br&gt;&lt;/div&gt; &lt;p&gt;&lt;strong&gt;Category:&lt;/strong&gt; &lt;span&gt;&lt;/span&gt;&lt;br&gt; &lt;p&gt;&lt;select id="FieldData3" name="FieldData3"&gt; &lt;option value=""&gt;Please select a category&lt;/option&gt; &lt;option value="a"&gt;a&lt;/option&gt; &lt;option value="b"&gt;b&lt;/option&gt; &lt;option value="c"&gt;c&lt;/option&gt; &lt;option value="d"&gt;d&lt;/option&gt; &lt;option value="e"&gt;e&lt;/option&gt; &lt;option value="f"&gt;f&lt;/option&gt; &lt;option value="other"&gt;Other&lt;/option&gt; &lt;/select&gt;&lt;label for="category"&gt;&lt;/label&gt; &lt;p&gt;&lt;div class=error id=questionError&gt;Please type your question in the box below:&lt;br&gt;&lt;/div&gt;&lt;label for="question"&gt;&lt;strong&gt;&lt;p&gt;Your Question:&lt;/strong&gt; &lt;span&gt;&lt;/span&gt;&lt;/label&gt;&lt;br&gt; &lt;textarea name="FieldData1" id="FieldData1" cols="50" rows="10"&gt;&lt;/textarea&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="submit" class="btn" value="Submit Question" name="Submit"&gt;&lt;/p&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p>Is the problem the order that I run the checks? I can't seem to figure this out. Any help would be appreciated.</p>
    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