<?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>dingobytes &#187; AJAX</title>
	<atom:link href="http://www.dingobytes.com/tag/ajax/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dingobytes.com</link>
	<description>what your nephew can't make you</description>
	<lastBuildDate>Fri, 11 Jun 2010 03:26:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Form Validation II</title>
		<link>http://www.dingobytes.com/tutorial/form-validation-ii</link>
		<comments>http://www.dingobytes.com/tutorial/form-validation-ii#comments</comments>
		<pubDate>Wed, 06 Feb 2008 04:48:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.dingobytes.com/2008/02/05/form-validation-ii/</guid>
		<description><![CDATA[Continuing on from the first part of the tutorial, we are going to continue through the list of form elements in the order we setup. Next we work on the email address. else if(document.getElementById("email").value=='' &#124;&#124; (document.getElementById("email").value!='' &#38;&#38; !emailfilter.test(document.getElementById("email").value))){ document.getElementById("error_msg").childNodes[0].data= 'Please enter a valid e-mail address.'; document.getElementById("error_msg").style.display = "block"; document.getElementById("email").focus(); return false; } Notice how we [...]]]></description>
			<content:encoded><![CDATA[<p>Continuing on from the first part of the tutorial, we are going to continue through the list of form elements in the order we setup. Next we work on the email address.</p>
<p><code><strong><font size="2" color="#0000c0"></p>
<p align="left">else<font size="2"> </font><strong><font size="2" color="#0000c0">if</font></strong><font size="2" color="#5c5c5c">(</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"email"</font><font size="2" color="#5c5c5c">).</font><font size="2">value</font><font size="2" color="#5c5c5c">==</font><font size="2" color="#005c00">''</font><font size="2"> </font><font size="2" color="#5c5c5c">||<br />
(</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"email"</font><font size="2" color="#5c5c5c">).</font><font size="2">value</font><font size="2" color="#5c5c5c">!=</font><font size="2" color="#005c00">''</font><font size="2"> </font><font size="2" color="#5c5c5c">&amp;&amp;<br />
!</font><font size="2">emailfilter</font><font size="2" color="#5c5c5c">.</font><font size="2">test</font><font size="2" color="#5c5c5c">(</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"email"</font><font size="2" color="#5c5c5c">).</font><font size="2">value</font><font size="2" color="#5c5c5c">))){<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"error_msg"</font><font size="2" color="#5c5c5c">).</font><font size="2">childNodes</font><font size="2" color="#5c5c5c">[</font><font size="2" color="#004080">0</font><font size="2" color="#5c5c5c">].</font><font size="2">data</font><font size="2" color="#5c5c5c">=<br />
</font><font size="2" color="#005c00">'Please enter a valid e-mail address.'</font><font size="2" color="#5c5c5c">;<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"error_msg"</font><font size="2" color="#5c5c5c">).</font><font size="2">style</font><font size="2" color="#5c5c5c">.</font><font size="2">display </font><font size="2" color="#5c5c5c">=</font><font size="2"> </font><font size="2" color="#005c00">"block"</font><font size="2" color="#5c5c5c">;<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"email"</font><font size="2" color="#5c5c5c">).</font><font size="2">focus</font><font size="2" color="#5c5c5c">();<br />
</font><strong><font size="2" color="#0000c0">return</font></strong><font size="2"> </font><font size="2" color="#800040">false</font><font size="2" color="#5c5c5c">;<br />
}</font></p>
<p></font></strong></code>Notice how we handle the email value checking first to see if it is empty to trigger an error and then using the logical operator OR (denoted as ||), we then look at another statement. In that statement we use the logical operator AND (denoted as &amp;&amp;) to trigger an error, and if both the email filed is not empty and the emailfilter.test is not valid then we do what is in between the curly brackets again. The contents are the same as what we described in the first statement.</p>
<p><span id="more-3"></span></p>
<p><code><strong><font size="2" color="#0000c0"></p>
<p align="left">else<font size="2"> </font><strong><font size="2" color="#0000c0">if</font></strong><font size="2" color="#5c5c5c">(</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"phone"</font><font size="2" color="#5c5c5c">).</font><font size="2">value</font><font size="2" color="#5c5c5c">!=</font><font size="2" color="#005c00">""</font><font size="2"> </font><font size="2" color="#5c5c5c">&amp;&amp;<br />
!</font><font size="2">phonefilter</font><font size="2" color="#5c5c5c">.</font><font size="2">test</font><font size="2" color="#5c5c5c">(</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"phone"</font><font size="2" color="#5c5c5c">).</font><font size="2">value</font><font size="2" color="#5c5c5c">)){<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"error_msg"</font><font size="2" color="#5c5c5c">).</font><font size="2">childNodes</font><font size="2" color="#5c5c5c">[</font><font size="2" color="#004080">0</font><font size="2" color="#5c5c5c">].</font><font size="2">data </font><font size="2" color="#5c5c5c">=<br />
</font><font size="2" color="#005c00">"Please enter valid phone number"</font><font size="2" color="#5c5c5c">;<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"error_msg"</font><font size="2" color="#5c5c5c">).</font><font size="2">style</font><font size="2" color="#5c5c5c">.</font><font size="2">display </font><font size="2" color="#5c5c5c">=</font><font size="2"> </font><font size="2" color="#005c00">"block"</font><font size="2" color="#5c5c5c">;<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"phone"</font><font size="2" color="#5c5c5c">).</font><font size="2">focus</font><font size="2" color="#5c5c5c">();<br />
</font><strong><font size="2" color="#0000c0">return</font></strong><font size="2"> </font><font size="2" color="#800040">false</font><font size="2" color="#5c5c5c">;<br />
}</font></p>
<p></font></strong></code>The phone is very similar to the email, but nothing is done if no phone number is entered.</p>
<p><code><strong><font size="2" color="#0000c0"></p>
<p align="left">else<font size="2"> </font><strong><font size="2" color="#0000c0">if</font></strong><font size="2" color="#5c5c5c">(</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"subject"</font><font size="2" color="#5c5c5c">).<br />
</font><font size="2">options</font><font size="2" color="#5c5c5c">[</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"subject"</font><font size="2" color="#5c5c5c">).</font><font size="2">selectedIndex</font><font size="2" color="#5c5c5c">].</font><font size="2">value</font><font size="2" color="#5c5c5c">==</font><font size="2" color="#005c00">''</font><font size="2" color="#5c5c5c">){<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"error_msg"</font><font size="2" color="#5c5c5c">).</font><font size="2">childNodes</font><font size="2" color="#5c5c5c">[</font><font size="2" color="#004080">0</font><font size="2" color="#5c5c5c">].</font><font size="2">data</font><font size="2" color="#5c5c5c">=<br />
</font><font size="2" color="#005c00">'Please choose a subject address.'</font><font size="2" color="#5c5c5c">;<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"error_msg"</font><font size="2" color="#5c5c5c">).</font><font size="2">style</font><font size="2" color="#5c5c5c">.</font><font size="2">display </font><font size="2" color="#5c5c5c">=</font><font size="2"> </font><font size="2" color="#005c00">"block"</font><font size="2" color="#5c5c5c">;<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"subject"</font><font size="2" color="#5c5c5c">).</font><font size="2">focus</font><font size="2" color="#5c5c5c">();<br />
</font><strong><font size="2" color="#0000c0">return</font></strong><font size="2"> </font><font size="2" color="#800040">false</font><font size="2" color="#5c5c5c">;<br />
}</font></p>
<p></font></strong></code>The subject is a handled a little differently as it is part of a select element. You will notice that the format in the IF statement changes to read the chosen value. Giving the default a value=&#8221;" allows the application to recognize when no subject is selected.</p>
<p><code><strong><font size="2" color="#0000c0"></p>
<p align="left">else<font size="2"> </font><strong><font size="2" color="#0000c0">if</font></strong><font size="2" color="#5c5c5c">(</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"content"</font><font size="2" color="#5c5c5c">).</font><font size="2">value</font><font size="2" color="#5c5c5c">==</font><font size="2" color="#005c00">''</font><font size="2" color="#5c5c5c">){<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"error_msg"</font><font size="2" color="#5c5c5c">).</font><font size="2">childNodes</font><font size="2" color="#5c5c5c">[</font><font size="2" color="#004080">0</font><font size="2" color="#5c5c5c">].</font><font size="2">data</font><font size="2" color="#5c5c5c">=</font><font size="2" color="#005c00">'Please be sure to enter comments.'</font><font size="2" color="#5c5c5c">;<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"error_msg"</font><font size="2" color="#5c5c5c">).</font><font size="2">style</font><font size="2" color="#5c5c5c">.</font><font size="2">display </font><font size="2" color="#5c5c5c">=</font><font size="2"> </font><font size="2" color="#005c00">"block"</font><font size="2" color="#5c5c5c">;<br />
</font><font size="2">document</font><font size="2" color="#5c5c5c">.</font><font size="2">getElementById</font><font size="2" color="#5c5c5c">(</font><font size="2" color="#005c00">"content"</font><font size="2" color="#5c5c5c">).</font><font size="2">focus</font><font size="2" color="#5c5c5c">();<br />
</font><strong><font size="2" color="#0000c0">return</font></strong><font size="2"> </font><font size="2" color="#800040">false</font><font size="2" color="#5c5c5c">;<br />
}</font><strong><font size="2" color="#0000c0">else</font></strong><font size="2"> </font><font size="2" color="#5c5c5c">{</font><font size="2"> </font><strong><font size="2" color="#0000c0">return</font></strong><font size="2"> </font><font size="2" color="#800040">true</font><font size="2" color="#5c5c5c">;</font><font size="2"> </font><font size="2" color="#5c5c5c">}<br />
}<br />
</font><font size="2" color="#008000">//--&gt;<br />
</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">script</font><font size="2" color="#0000ff">&gt;</font></p>
<p></font></strong></p>
<p align="left">The last item for textarea element is similar to what was done for the name element, and that is all followed the last ELSE which will return a true or you can have it point to another function (like we will do for AJAX call in the final example). If set to true, it will just submit the form.</p>
<p></code>So now that the script is done, the only thing left is providing a place to display the error. The div/span can be placed where ever you want the error to be displayed. The error will only be displayed when triggered by the javascript.</p>
<p><code><font size="2" color="#0000ff">&lt;<font size="2" color="#a31515">span</font><font size="2"> </font><font size="2" color="#ff0000">id</font><font size="2">=</font><font size="2" color="#0000ff">"error_msg"</font><font size="2"> </font><font size="2" color="#ff0000">class</font><font size="2">=</font><font size="2" color="#0000ff">"errormssg"&gt;</font><font size="2"> </font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">span</font><font size="2" color="#0000ff">&gt;</font></p>
<p></font></code>The trick here is when the id error_message style is defined. The key will be to make the class display none. The javascript will trigger the error to be visible (by changing display:block;) and then the error can be changed back by simply changing the style for id=&#8221;error_msg&#8221; display:block.</p>
<p>So lets take a look at the finished project. You can view and copy the <a href="http://www.dingobytes.com/example/testform.html" title="Example Form">finished product here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dingobytes.com/tutorial/form-validation-ii/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
