<?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; Tutorial</title>
	<atom:link href="http://www.dingobytes.com/tag/tutorial/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>Replacing text in multiple files</title>
		<link>http://www.dingobytes.com/tutorial/replacing-text-in-multiple-files</link>
		<comments>http://www.dingobytes.com/tutorial/replacing-text-in-multiple-files#comments</comments>
		<pubDate>Wed, 12 May 2010 16:02:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[multiple files]]></category>
		<category><![CDATA[replace]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.dingobytes.com/?p=120</guid>
		<description><![CDATA[Unfortunately during testing I have had some typo's and the generated report stored in the directory structure did not display as intended. The changes needed really didn't warrant processing the script again and adding more useless entries into the database, so I decided to change the script and then update the html in the report.]]></description>
			<content:encoded><![CDATA[<p>Recently I took a new job and it involves a lot of report generating. This has been a great fit for me and I have enjoyed the short time here in St Paul, MN.</p>
<p>The process for the current project is to pull a *.csv file from an email account, parse it into a database query/queries, generate html code and then store the html in a directory structure while also storing the report information in the database.</p>
<p><span id="more-120"></span></p>
<p>Unfortunately during testing I have had some typo&#8217;s and the generated report stored in the directory structure did not display as intended. The changes needed really didn&#8217;t warrant processing the script again and adding more useless entries into the database, so I decided to change the script and then update the html in the report.</p>
<p>Having over 100 reports generated, I needed a simple script to do this. I had done this with bash before, but I thought there had to be an easier way then looping through a for statement, saving the changes to a new file and then moving the new file to replace the old.</p>
<p>In comes Perl. Looking through some posts, I found that you can edit text in the file with Perl. The command goes something like this:</p>
<p><code>perl -pi -w -e 's/search/replace/g;' *.html</code></p>
<p>Here is a further explanation of what is going on. </p>
<p><code>-p assume loop (like -n) but print line also, like sed<br />
-i edit <> files in place (makes backup if extension supplied)<br />
-w enable many useful warnings (RECOMMENDED)<br />
-e execute the follwing line of code</code></p>
<p>The code referred to after the -e is just a search and replace regular expression. </p>
<p>The &#8216;s/&#8217; means to search and start your regular expression to search for. </p>
<p>You then enter your expression that you want to search for. You need to make sure you escape out special characters with the back-slash [\]. The forward slash is used to separate what you are searching for and what you want to replace. The replace section is an expression as well and you need to treat special characters the same way.</p>
<p>Finally you have the last forward slash indicates the end of your replace expression and the &#8216;g&#8217; indicates global.</p>
<p>Here is an example of how I would change the directory used for images from &#8216;/image/&#8217; to &#8216;images/&#8217; for all the files in a directory with the extension of .html.</p>
<p><code>perl -pi -w -e 's/\/image\//images\//g;' *.html</code></p>
<p>That is how you do it. If you have to go through multiple directories, you could use a bash or perl command to loop through those directories. Hope it didn&#8217;t confuse you too much.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dingobytes.com/tutorial/replacing-text-in-multiple-files/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Show Message 1.0- jQuery Plugin for displaying messages</title>
		<link>http://www.dingobytes.com/tutorial/show-message-1-0-jquery-plugin-for-displaying-messages</link>
		<comments>http://www.dingobytes.com/tutorial/show-message-1-0-jquery-plugin-for-displaying-messages#comments</comments>
		<pubDate>Mon, 01 Feb 2010 02:26:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[notification box]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.dingobytes.com/?p=90</guid>
		<description><![CDATA[Inspired by a similar feature at Twitter.com, this jQuery Plugin will help you display messages at the top of your page with an assortment of options. Download the latest version 2.1 of the plugin at http://showMessage.dingobytes.com/download/ Please see the site, as the information below is for previous versions. This has been updated! This is my [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by a similar feature at Twitter.com, this jQuery Plugin will help you display messages at the top of your page with an assortment of options.</p>
<p style="border:medium #06f solid;width:95%;padding:0.5em;font-size:x-large;text-align:center;">Download the latest version 2.1 of the plugin at <a href="http://showMessage.dingobytes.com/download/" title="download">http://showMessage.dingobytes.com/download/</a></p>
<p><strong><em>Please see the site, as the information below is for previous versions. This has been updated!</em></strong></p>
<p>This is my first attempt at a jQuery Plugin, so although it works fine for my liking, I am sure there are many things that can be done more effectively.</p>
<ul>
<li>Message displayed at the top of page (no scrolling to view error)</li>
<li>Multiple messages can be displayed</li>
<li>Options to automatically close the message after set delay</li>
<li>Message closes with blur/&#8217;esc&#8217; key/&#8217;close&#8217; link</li>
</ul>
<ul>
<li><a title="Download" href="#download">Download</a></li>
<li><a title="Install" href="#install">Install</a></li>
<li><a title="Options" href="#options">Options</a></li>
<li><a title="Examples" href="#examples">Examples</a></li>
</ul>
<div id="download">
<h2><span id="more-90"></span>Download</h2>
<p style="border: medium #06f solid; width: 95%; padding: 0.5em; font-size: x-large; text-align: center;"><a title="download now" href="http://showMessage.dingobytes.com/download/" target="_blank">Download showMessage 1.1</a></p>
</div>
<div id="install">
<h2>Install</h2>
<p>You can install showMessage jQuery Plugin by including the script somewhere in the page AFTER jQuery. This plugin should work on jQuery version 1.3.2+.</p>
<pre class="brush: html">&lt;script type=&quot;text/javascript&quot; src=&quot;/path/to/jquery.showMessage.min.js&quot;
charset=&quot;utf-8&quot;&gt;&lt;\/script&gt;</pre>
<p>If you wish to style the text, the message is displayed through an unordered list in the div with id attribute=showMessage</p>
<pre class="brush: css">#showMessage li
{
font-family: Arial, Helvetica, clean, sans-serif;
font-size: large;
font-weight: bold;
color: red;
}</pre>
</div>
<div id="options">
<h2>Options</h2>
<p>Here are the possible options with their default values:</p>
<pre class="brush: javascript">
thisMessage:		[&#039;&#039;],
backgroundColor:	&#039;#F7F7F7&#039;,
color:			&#039;#000000&#039;,
opacity:		90,
displayNavigation:	true,
autoClose:		false,
delayTime:		5000
</pre>
<dl>
<dt>thisMessage <em>(required)</em><span class="alignright">[Array]</span></dt>
<dd>The message you will be returning to the page. The message is in the form of an array.</dd>
<dt>blackgroundColor<span class="alignright">[string]</span></dt>
<dd>Default set to &#8216;#F7F7F7&#8242;. The background color of the message holder. (&#8216;#F7F7F7&#8242; or &#8216;rgb(247, 247, 247)&#8217; or &#8216;white&#8217;)</dd>
<dt>color<span class="alignright">[string]</span></dt>
<dd>Default set to &#8216;#000000&#8242;. The font color in the message holder. (&#8216;#000000&#8242; or &#8216;rgb(0, 0, 0)&#8217; or &#8216;black&#8217;)</dd>
<dt>opacity<span class="alignright">[integer]</span></dt>
<dd>Default set to 90. Opacity of the message holder. Value is an integer between 1 and 100.</dd>
<dt>displayNavigation<span class="alignright">[boolean]</span></dt>
<dd>Default set to true. Will display the &#8216;esc&#8217; and &#8216;close&#8217; information in upper right.</dd>
<dt>autoClose<span class="alignright">[boolean]</span></dt>
<dd>Default is set to false. When set to true, it will close the message holder after specified delay time.</dd>
<dt>delayTime<span class="alignright">[integer]</span></dt>
<dd>Default is set to 5000. Amount of time in milli-seconds before the message holder will close/hide (autoClose must be set to true).</dd>
</dl>
</div>
<div id="examples">
<h2>Example</h2>
<dl>
<dt>Simple</dt>
<dd> Just return a simple message to message holder.</p>
<pre class="brush: javascript">
jQuery.showMessage({
&#039;thisMessage&#039;:[&#039;&lt;mg src=&quot;/images/accept.png&quot; alt=&quot;accept&quot;/&gt;successful!&#039;]
});
</pre>
</dd>
<dt>Advanced</dt>
<dd> This should look a lot like Twitter.</p>
<pre class="brush: javascript">
var returnMessage = [&#039;Account was created successfully!&#039;,&#039;Thank you.&#039;];
jQuery.showMessage({
&#039;thisMessage&#039;:		returnMessage,
&#039;backgroundColor&#039;:	&#039;#f7f7f7&#039;,
&#039;color&#039;:		&#039;black&#039;,
&#039;opacity&#039;:		95,
&#039;displayNavigation&#039;:	false,
&#039;autoClose&#039;:		true,
&#039;delayTime&#039;:		6000
});
</pre>
</dd>
</dl>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.dingobytes.com/tutorial/show-message-1-0-jquery-plugin-for-displaying-messages/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Lame Mouse Over Effect</title>
		<link>http://www.dingobytes.com/tutorial/lame-mouse-over-effect</link>
		<comments>http://www.dingobytes.com/tutorial/lame-mouse-over-effect#comments</comments>
		<pubDate>Thu, 03 Apr 2008 07:07:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[effect]]></category>
		<category><![CDATA[mouseover]]></category>

		<guid isPermaLink="false">http://www.dingobytes.com/?p=8</guid>
		<description><![CDATA[So at work today, I wanted to see if I could create a lame mouse over effect to let feel like they were highlighting a listing. I wanted to pop out the moused over listing on the page by putting a thin border around the moused over div and then change the background. I was [...]]]></description>
			<content:encoded><![CDATA[<p>So at work today, I wanted to see if I could create a lame mouse over effect to let feel like they were highlighting a listing.  I wanted to pop out the moused over listing on the page by putting a thin border around the moused over div and then change the background. I was able to create the effect with javascript and coldfusion very easily. Because I don&#8217;t host this site on a coldfusion server, I will just show the javascript with some rendered HTML.</p>
<p><span id="more-8"></span>To save some time, I will explain the javascript and how to call it. You can grab the css from the example page.</p>
<pre id="line149">
<pre class="brush: javascript">
&lt;script type=&quot;text/javascript&quot;&gt;
//&lt;![CDATA[
var listing = {

g:function(id) {
    return document.getElementById(id);
},

// function to change the background and border of listing on mouse over
highlight:function(id) {
    listing_el = listing.g(&quot;listing&quot; + id);
    if(id != 1) { //this is what to do for all divs other then first one
        id_above = id - 1;
        listing_above_el = listing.g(&quot;listing&quot; + id_above);
        listing_above_el.style.border = &quot;#fff 2px solid&quot;; //removes border on result above highlighted
        listing_el.style.backgroundColor = &quot;#fffae7&quot;;
        listing_el.style.border = &quot;#837b97&quot;;
    }
    else {
        listing_el.className = &quot;highlight_el&quot;;
        listing_el.style.backgroundColor = &quot;#fffae7&quot;;
        listing_el.style.border = &quot;#837b97&quot;;
    }
},

normal:function(id) {
    listing_el = listing.g(&quot;listing&quot; + id);
    if(id != 1) {
        id_above = id - 1;
        listing_above_el = listing.g(&quot;listing&quot; + id_above);
        listing_above_el.style.borderBottom = &quot;#cccccc 2px dotted&quot;; //restores border to result above highlighted
	listing_el.style.backgroundColor = &quot;#ffffff&quot;;
	listing_el.style.border = &quot;#ffffff 2px solid&quot;;
        listing_el.style.borderBottom = &quot;#cccccc 2px dotted&quot;;
    }
    else {
        listing_el.className = &quot;normal_el&quot;;
        listing_el.style.backgroundColor = &quot;#ffffff&quot;;
	listing_el.style.border = &quot;#ffffff 2px solid&quot;;
        listing_el.style.borderBottom = &quot;#cccccc 2px dotted&quot;;
    }
}
//]]&gt;
&lt;/script&gt;
</pre>
</pre>
<p>The HTML can be spit through some query or loop and is straight forward.</p>
<p><code>
<pre class="brush: html">
&lt;div id=&quot;listing1&quot; class=&quot;listing&quot; onmouseover=&quot;listing.highlight(&#039;1&#039;);&quot; onmouseout=&quot;listing.normal(&#039;1&#039;);&quot;&gt;
&lt;div id=&quot;listing_top&quot;&gt;
&lt;span class=&quot;left&quot;&gt;
&lt;a href=&quot;javascript:void(0);&quot; title=&quot;121 Initech Rd&quot;&gt;121 Initech Rd&lt;/a&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;div id=&quot;listing_left&quot;&gt;
&lt;a href=&quot;javascript:void(0);&quot; title=&quot;121 Initech Rd&quot;&gt;&lt;img src=&quot;images/inventory/thumbs/0/0/0/000976.jpg&quot; alt=&quot;121 Initech Rd&quot; class=&quot;displayed&quot; width=&quot;151&quot; /&gt;&lt;/a&gt;
2.00 bed 3.00 bath 1296 sqft&lt;br /&gt;
Lake Home&lt;br /&gt;
Listing: 08-1979
&lt;/div&gt;
&lt;div id=&quot;listing_right&quot;&gt;&lt;span class=&quot;large_price&quot;&gt;$198699&lt;/span&gt; One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. &quot;What&#039;s happened to me? &quot; he thought. It wasn&#039;t a dream.&lt;/div&gt;
&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
&lt;/div&gt;&lt;/code&gt;&lt;/code&gt;

&lt;code&gt;&lt;div id=&quot;listing2&quot; class=&quot;listing&quot; onmouseover=&quot;listing.highlight(&#039;2&#039;);&quot; onmouseout=&quot;listing.normal(&#039;2&#039;);&quot;&gt;
&lt;div id=&quot;listing_top&quot;&gt;
&lt;span class=&quot;left&quot;&gt;
&lt;a href=&quot;javascript:void(0);&quot; title=&quot;121 Initech Rd&quot;&gt;121 Initech Rd&lt;/a&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;div id=&quot;listing_left&quot;&gt;
&lt;a href=&quot;javascript:void(0);&quot; title=&quot;121 Initech Rd&quot;&gt;&lt;img src=&quot;images/inventory/thumbs/0/0/0/000976.jpg&quot; alt=&quot;121 Initech Rd&quot; class=&quot;displayed&quot; width=&quot;151&quot; /&gt;&lt;/a&gt;
2.00 bed 3.00 bath 1296 sqft&lt;br /&gt;
Lake Home&lt;br /&gt;
Listing: 08-1979
&lt;/div&gt;
&lt;div id=&quot;listing_right&quot;&gt;&lt;span class=&quot;large_price&quot;&gt;$198699&lt;/span&gt; His room, a proper human room although a little too small, lay peacefully between its four familiar walls. A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer.&lt;/div&gt;
&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
<p>This can be completed repeated and by dynamically changing the value <em>i</em> you can populate the results so that</p>
<p><code>[sourcecode language='html']<br />
&lt;div id="listing<em>i</em>" class="listing" onmouseover="listing.highlight('<em>i</em>');" onmouseout="listing.normal('<em>i</em>');"&gt;</code></p>
<p>You can see the <a title="finished product" href="http://www.dingobytes.com/example/results.html" target="_blank">finished product here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dingobytes.com/tutorial/lame-mouse-over-effect/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>client side form validation</title>
		<link>http://www.dingobytes.com/tutorial/hello-world</link>
		<comments>http://www.dingobytes.com/tutorial/hello-world#comments</comments>
		<pubDate>Thu, 31 Jan 2008 01:22:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.dingobytes.com/?p=1</guid>
		<description><![CDATA[The more we learn the more fun we have. Take for example the simple process of validating a form. We can save the user a lot of time validating the form on the client side and still do it without those annoying alerts that pop up with a loud DING! It is rather easy with [...]]]></description>
			<content:encoded><![CDATA[<p>The more we learn the more fun we have. Take for example the simple process of validating a form. We can save the user a lot of time validating the form on the client side and still do it without those annoying alerts that pop up with a loud DING! It is rather easy with the help of a little javascript.</p>
<p>Now depending on what type of data you are passing to the server with your form, you will often need to also validate your fields data on the server too. This tutorial will only look at the client side validation, leaving server side validation to another tutorial at some other time.</p>
<p>Lets first start with our form. We are going to do a simple form that just asks for name, email address, optional phone number, a select list for subject and text area for message. Oh, I always forget, we need a submit and clear button too. Lets start it all of with our form tag.</p>
<p><span id="more-1"></span></p>
<p>The form tag <code>&lt;form&gt;</code> will typically have the standard attributes of a method (get or post) and some sort of action. I often use AJAX to process my forms so the action may simply be action=&#8221;#&#8221; or action =&#8221;javascript:void();&#8221;. Knowing that we will be validating this form we are going to add one more attribute to the form element to validate the form when it is submitted and it is obviously called onsubmit. Here is the tag we will be using.</p>
<p><code><font size="2" color="#0000ff"></p>
<p align="left">&lt;<font size="2" color="#a31515">form</font><font size="2"> </font><font size="2" color="#ff0000">action</font><font size="2">=</font><font size="2" color="#0000ff">"javascript:void(0);"</font><font size="2"> </font><font size="2" color="#ff0000">name</font><font size="2">=</font><font size="2" color="#0000ff">"myform"<br />
</font><font size="2" color="#ff0000">onsubmit</font><font size="2">="</font><strong><font size="2" color="#0000c0">return</font></strong><font size="2"> validateForm</font><font size="2" color="#5c5c5c">();</font><font size="2">"</font><font size="2" color="#0000ff">&gt;</font></p>
<p></font></code>If you look what is inside the onsubmit attribute, it is pretty straight forward, we want to return the result of the javascript function we are going to create called validateForm() so that if it is true the form will will submit and if it is false it will not send.</p>
<p>Next we are going to just briefly go over the other form elements. There are several other elements, but for todays tutorial we are only going to use the &lt;input&gt;, &lt;select&gt; and &lt;textarea&gt; elements. The one thing to remember whenever using these elements is they should always have a name and id attribute and the input element should have a type attribute as well. Here is a an example of one of the input elements (for email) and our select and textarea elements.</p>
<p><code><font size="2" color="#0000ff"></p>
<p align="left">&lt;<font size="2" color="#a31515">label</font><font size="2"> </font><font size="2" color="#ff0000">for</font><font size="2">=</font><font size="2" color="#0000ff">"name"&gt;</font><font size="2">Name:</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">label</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">input</font><font size="2"> </font><font size="2" color="#ff0000">type</font><font size="2">=</font><font size="2" color="#0000ff">"text"</font><font size="2"> </font><font size="2" color="#ff0000">id</font><font size="2">=</font><font size="2" color="#0000ff">"name"</font><font size="2"> </font><font size="2" color="#ff0000">name</font><font size="2">=</font><font size="2" color="#0000ff">"name"</font><font size="2"> </font><font size="2" color="#0000ff">/&gt;<br />
&lt;</font><font size="2" color="#a31515">label</font><font size="2"> </font><font size="2" color="#ff0000">for</font><font size="2">=</font><font size="2" color="#0000ff">"email"&gt;</font><font size="2">Email:</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">label</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">input</font><font size="2"> </font><font size="2" color="#ff0000">type</font><font size="2">=</font><font size="2" color="#0000ff">"text"</font><font size="2"> </font><font size="2" color="#ff0000">id</font><font size="2">=</font><font size="2" color="#0000ff">"email"</font><font size="2"> </font><font size="2" color="#ff0000">name</font><font size="2">=</font><font size="2" color="#0000ff">"email"</font><font size="2"> </font><font size="2" color="#0000ff">/&gt;<br />
&lt;</font><font size="2" color="#a31515">label</font><font size="2"> </font><font size="2" color="#ff0000">for</font><font size="2">=</font><font size="2" color="#0000ff">"phone"&gt;</font><font size="2">Phone:</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">label</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">input</font><font size="2"> </font><font size="2" color="#ff0000">type</font><font size="2">=</font><font size="2" color="#0000ff">"text"</font><font size="2"> </font><font size="2" color="#ff0000">id</font><font size="2">=</font><font size="2" color="#0000ff">"phone"</font><font size="2"> </font><font size="2" color="#ff0000">name</font><font size="2">=</font><font size="2" color="#0000ff">"phonel"</font><font size="2"> </font><font size="2" color="#0000ff">/&gt;<br />
&lt;</font><font size="2" color="#a31515">label</font><font size="2"> </font><font size="2" color="#ff0000">for</font><font size="2">=</font><font size="2" color="#0000ff">"subject"&gt;</font><font size="2">Subject:</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">label</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">select</font><font size="2"> </font><font size="2" color="#ff0000">id</font><font size="2">=</font><font size="2" color="#0000ff">"subject"</font><font size="2"> </font><font size="2" color="#ff0000">name</font><font size="2">=</font><font size="2" color="#0000ff">"subject"&gt;<br />
&lt;</font><font size="2" color="#a31515">option</font><font size="2"> </font><font size="2" color="#ff0000">value</font><font size="2">=</font><font size="2" color="#0000ff">"Advertising Inquiry"&gt;</font><font size="2">Advertising Inquiry</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">option</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">option</font><font size="2"> </font><font size="2" color="#ff0000">value</font><font size="2">=</font><font size="2" color="#0000ff">"Support Inquiry"&gt;</font><font size="2">Support Inquiry</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">option</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">option</font><font size="2"> </font><font size="2" color="#ff0000">value</font><font size="2">=</font><font size="2" color="#0000ff">"General Inquiry"&gt;</font><font size="2">General Inquiry</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">option</font><font size="2" color="#0000ff">&gt;<br />
&lt;/</font><font size="2" color="#a31515">select</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">label</font><font size="2"> </font><font size="2" color="#ff0000">for</font><font size="2">=</font><font size="2" color="#0000ff">"message"&gt;</font><font size="2">Message:</font><font size="2" color="#0000ff">&lt;/</font><font size="2" color="#a31515">label</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">textarea</font><font size="2"> </font><font size="2" color="#ff0000">id</font><font size="2">=</font><font size="2" color="#0000ff">"message"</font><font size="2"> </font><font size="2" color="#ff0000">name</font><font size="2">=</font><font size="2" color="#0000ff">"message"</font><font size="2"> </font><font size="2" color="#ff0000">rows</font><font size="2">=</font><font size="2" color="#0000ff">""</font><font size="2"> </font><font size="2" color="#ff0000">cols</font><font size="2">=</font><font size="2" color="#0000ff">""&gt;&lt;/</font><font size="2" color="#a31515">textarea</font><font size="2" color="#0000ff">&gt;<br />
&lt;</font><font size="2" color="#a31515">input</font><font size="2"> </font><font size="2" color="#ff0000">type</font><font size="2">=</font><font size="2" color="#0000ff">"submit"</font><font size="2"> </font><font size="2" color="#ff0000">id</font><font size="2">=</font><font size="2" color="#0000ff">"submit"</font><font size="2"> </font><font size="2" color="#ff0000">name</font><font size="2">=</font><font size="2" color="#0000ff">"submit"</font><font size="2"> </font><font size="2" color="#ff0000">value</font><font size="2">=</font><font size="2" color="#0000ff">"Submit"</font><font size="2"> </font><font size="2" color="#0000ff">/&gt;</font></p>
<p></font></p>
<p align="left">Now that the form is created, we can move on to the javascript function to validate the form. You will want to place this javascript before the form in the page. Here is the code, I will explain it as we go through it.</p>
<p></code><code><font size="2" color="#0000ff"></p>
<p align="left">&lt;<font size="2" color="#a31515">script</font><font size="2"> </font><font size="2" color="#ff0000">type</font><font size="2">=</font><font size="2" color="#0000ff">"text/javascript"&gt;<br />
</font><font size="2" color="#008000">&lt;!--//<br />
</font><strong><font size="2" color="#0000c0">function</font></strong><font size="2"> validateForm</font><font size="2" color="#5c5c5c">(){<br />
</font><strong><font size="2" color="#0000c0">var</font></strong><font size="2"> emailfilter </font><font size="2" color="#5c5c5c">=<br />
</font><font size="2" color="#800040">/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/</font><font size="2" color="#5c5c5c">;<br />
</font><strong><font size="2" color="#0000c0">var</font></strong><font size="2"> phonefilter </font><font size="2" color="#5c5c5c">=<br />
</font><font size="2" color="#800040">/\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/</font><font size="2" color="#5c5c5c">;<br />
</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">"name"</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 enter full name"</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">"name"</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></code>The first thing we did was declare that we were going to use javascript by using the &lt;script&gt; tag. We then defined the function validateForm() which will have no values passed to it and use the curly brackets to hold our statements for the function.</p>
<p>Two regular expressions (regex) are then defined which will be used for validatig the email address and the pone number. I have created these two regex items, but you can find about any regex you could imagine at <a href="http://www.regexlib.com">http://www.regexlib.com</a>.</p>
<p>The next steps are going to be repeated in slightly different ways, so we will discuss them here in a little more detail. The first statement starts with the IF statement where we check to see if what is inside the () is true. If the statement is true it continues to process what is in the curly brackets.</p>
<p>The text on the right hand side of the first statement replaces the childNodes.data of the element with id=&#8221;error_message&#8221;. This is followed by style change of the element, in this example we change display:none to display:block.</p>
<p>To make it less confusing for the user, the next statement focuses the cursor back in the input field with id =&#8221;name&#8221;.  The last item in the IF statement is return false which will stop the form from processing. The IF statement is then closed with the end curly bracket.</p>
<p> To be continued. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dingobytes.com/tutorial/hello-world/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
