<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Random Snippets &#187; email form</title>
	<atom:link href="http://www.randomsnippets.com/tag/email-form/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Form verification via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 07:27:12 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[email form]]></category>
		<category><![CDATA[error warnings]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[form email]]></category>
		<category><![CDATA[onSubmit]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[verifyForm]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/</guid>
		<description><![CDATA[
function verifyForm(form) {
    var userName = form.name.value;
    var userEmail = form.email.value;
    var success = 1;
    if (!userName) {
        document.getElementById("usernameMsg").style.display = "";
        form.name.style.backgroundColor = "yellow";
      [...]]]></description>
			<content:encoded><![CDATA[<p><script language="javascript">
function verifyForm(form) {
    var userName = form.name.value;
    var userEmail = form.email.value;
    var success = 1;
    if (!userName) {
        document.getElementById("usernameMsg").style.display = "";
        form.name.style.backgroundColor = "yellow";
        form.name.style.border = "3px red solid";
        success = 0;
    }
    else {
        form.name.style.backgroundColor = "";
        form.name.style.border = "";
        document.getElementById("usernameMsg").style.display = "none";
    }
    if (!userEmail) {
        document.getElementById("emailMsg").style.display = "block";
        form.email.style.backgroundColor = "yellow";
        form.email.style.border = "3px red solid";
        success = 0;
    }
    else {
        form.email.style.backgroundColor = "";
        form.email.style.border = "";
        document.getElementById("emailMsg").style.display = "none";
    }
    if(!success) {
        alert("The form is incomplete.  Please read the error message(s).");
        return false;
    }
    else {
        alert("The form was submitted succesfully!");
        return true;
    }
}
</script><br />
Are you looking for a simple way to verify a form that you have?  The example below demonstrates some of the common techniques used in verifying a form.  Test out the form by leaving at least one of the fields blank before you submit the form.  </p>
<fieldset>
<legend>Demo</legend>
<form method="POST" onSubmit="return verifyForm(this);">
Username<br />
<input type="text" name="name">
<div id="usernameMsg" style="display:none;"><span style="color:red;">Please fill in your username.</span></div>
<p>
Email<br />
<input type="email" name="email">
<div id="emailMsg" style="display:none;"><span style="color:red;">Please fill in your email.</span></div>
<p></p>
<input type="submit" value="Submit">
</form>
</fieldset>
<p><span id="more-8"></span><br />
Here is the plain HTML for the form:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;POST&quot; onSubmit=&quot;return verifyForm(this);&quot;&gt;
Username&lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;&lt;div id=&quot;usernameMsg&quot; style=&quot;display:none;&quot;&gt;&lt;span style=&quot;color:red;&quot;&gt;Please fill in your username.&lt;/span&gt;&lt;/div&gt;
&lt;br&gt;
Email&lt;input type=&quot;email&quot; name=&quot;email&quot;&gt;&lt;div id=&quot;emailMsg&quot; style=&quot;display:none;&quot;&gt;&lt;span style=&quot;color:red;&quot;&gt;Please fill in your email.&lt;/span&gt;&lt;/div&gt;
&lt;br&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;</pre></div></div>

<p>Notice that the error warnings such as the <b>Please fill in your username.</b> or <b>Please fill in your email.</b> have already been preloaded but hidden from the viewer by setting the <code>display</code> attribute to <code>none</code>.  If the user submits the form with one or more of the fields blank, 4 things will happen to notify the user (Note: I tried to squeeze in as many warning events as possible just for demonstration purposes so I apologize if it is a bit over the top):</p>
<ol>
<li>the background of the missing input field will turn yellow</li>
<li>a red border will appear around the missing input field</li>
<li>the hidden error message will become visible</li>
<li>an alert will pop up notifying the user that 1 or more fields are missing</li>
</ol>
<p>When the user clicks on the <b>Submit</b> button, the <b>onSubmit</b> event gets triggered and executes the <b>verifyForm()</b> JavaScript function.  If the <b>onSubmit</b> event returns a <code>false</code> value, the form does not get submitted.  This value is determined by the <b>verifyForm()</b> function, which returns <code>true</code> if all fields are filled in and returns <code>false</code> if one or more fields are missing.   </p>
<p>Here is a peek at the JavaScript <b>verifyForm()</b> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> verifyForm<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> userName <span style="color: #339933;">=</span> form.<span style="color: #000066;">name</span>.<span style="color: #660066;">value</span>;
    <span style="color: #003366; font-weight: bold;">var</span> userEmail <span style="color: #339933;">=</span> form.<span style="color: #660066;">email</span>.<span style="color: #660066;">value</span>;
    <span style="color: #003366; font-weight: bold;">var</span> success <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span>;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>userName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;usernameMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span>;
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;yellow&quot;</span>;
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3px red solid&quot;</span>;
        success <span style="color: #339933;">=</span> 0;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;usernameMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>userEmail<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;emailMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span>;
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;yellow&quot;</span>;
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3px red solid&quot;</span>;
        success <span style="color: #339933;">=</span> 0;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;emailMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>success<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The form is incomplete.  Please read the error message(s).&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The form was submitted succesfully!&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Lines 5-15 checks to see if the username field is blank or not.  If the field is blank, it uses the DOM to access the div that is holding our warning message to the user and sets the <code>display</code> attribute to <code>block</code> (line 6).  This makes the message visible to the user.  </p>
<p>Next, it accesses the input text field and dynamically changes the <code>backgroundColor</code> attribute to <code>yellow</code> (line 7). </p>
<p>Then, it accesses the input text field again and dynamically changes the <code>border</code> attribute to <code>3px red solid</code> (line 8).  Here is a quick breakdown of the attribute we just used:</p>
<ul>
<li>3px &#8211; the width of the border</li>
<li>red &#8211; the color of the border</li>
<li>solid &#8211; the style of the border</li>
</ul>
<p>Finally, it sets the <code>success</code> variable to <code>0</code> which is equivalent to a <code>false</code> value (line 9).  We will use this variable later on to decide if the form is ready to be submitted or not.</p>
<p>The <code>else</code> statement in lines 11-14 just resets all the styling back to the default values.  This is for the case where a user is resubmitting the form so we want to remove all the warnings just in case they were activated from any previous submissions.</p>
<p>Lines 16-26 does the same exact thing except it is for the email text input field.  Remember, all we are doing in this example is checking to see if the field is empty or not.  This is to keep the code simple.</p>
<p>Line 27 checks the <code>success</code> variable to see if it had been set to false.  If so, an alert is executed telling the user that one or more fields were missing and to check the error messages (line 28).  Then it returns a <code>false</code> value to prevent the form from being submitted (line 29).</p>
<p>If the form has been filled-in completely, the <code>else</code> block gets executed in lines 31-34.  In this case, an alert is executed saying that the form was submitted successfully.  As a resutl, the function returns the value <code>true</code> which then submits the form.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
