<?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; forms</title>
	<atom:link href="http://www.dingobytes.com/tag/forms/feed" rel="self" type="application/rss+xml" />
	<link>http://www.dingobytes.com</link>
	<description>what your nephew can't make you</description>
	<lastBuildDate>Tue, 17 Jan 2012 22:24:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</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. Notice how we handle the email value checking first to see if it is empty to trigger an error and then using the [...]]]></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>
<pre class="brush: jscript; title: ; notranslate">
else if(document.getElementById(&quot;email&quot;).value=='' || (document.getElementById(&quot;email&quot;).value!='' &amp;amp;&amp;amp; !emailfilter.test(document.getElementById(&quot;email&quot;).value))) {
document.getElementById(&quot;error_msg&quot;).childNodes[0].data='Please enter a valid e-mail address.';
document.getElementById(&quot;error_msg&quot;).style.display = &quot;block&quot;;
document.getElementById(&quot;email&quot;).focus();
return false;
}
</pre>
<p>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>
<pre class="brush: jscript; title: ; notranslate">
else if(document.getElementById(&quot;phone&quot;).value!=&quot;&quot; &amp;amp;&amp;amp; !phonefilter.test(document.getElementById(&quot;phone&quot;).value)) {
document.getElementById(&quot;error_msg&quot;).childNodes[0].data =&quot;Please enter valid phone number&quot;;
document.getElementById(&quot;error_msg&quot;).style.display = &quot;block&quot;;
document.getElementById(&quot;phone&quot;).focus();
return false;
}
</pre>
<p>The phone is very similar to the email, but nothing is done if no phone number is entered.</p>
<pre class="brush: jscript; title: ; notranslate">
else if(document.getElementById(&quot;subject&quot;).options[document.getElementById(&quot;subject&quot;).selectedIndex].value=='') {
document.getElementById(&quot;error_msg&quot;).childNodes[0].data='Please choose a subject address.';
document.getElementById(&quot;error_msg&quot;).style.display = &quot;block&quot;;
document.getElementById(&quot;subject&quot;).focus();
return false;
}
</pre>
<p>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>
<pre class="brush: jscript; title: ; notranslate">
else if(document.getElementById(&quot;content&quot;).value=='') {
document.getElementById(&quot;error_msg&quot;).childNodes[0].data='Please be sure to enter comments.';
document.getElementById(&quot;error_msg&quot;).style.display = &quot;block&quot;;
document.getElementById(&quot;content&quot;).focus();
return false;
}else { return true; }
}
//--&gt;
</pre>
<p>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>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>
<pre class="brush: jscript; title: ; notranslate">
&lt;div id=&quot;error_msg&quot; class=&quot;errormsg&quot; style=&quot;display:none;&quot;&gt;&amp;nbsp;&lt;/div&gt;
</pre>
<p>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 title="Example Form" href="http://www.dingobytes.com/example/testform.html">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>The form tag
<pre class="brush: xml; title: ; notranslate">&lt;form&gt;</pre>
<p> 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>
<pre class="brush: xml; title: ; notranslate">
&lt;form action=&quot;javascript:void(0);&quot; name=&quot;myform&quot; onsubmit=&quot;return validateForm();&quot;&gt;
</pre>
<p>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>
<pre class="brush: xml; title: ; notranslate">
&lt;label for=&quot;name&quot;&gt;Name:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;name&quot; name=&quot;name&quot; /&gt;
&lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;email&quot; name=&quot;email&quot; /&gt;
&lt;label for=&quot;phone&quot;&gt;Phone:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;phone&quot; name=&quot;phonel&quot; /&gt;
&lt;label for=&quot;subject&quot;&gt;Subject:&lt;/label&gt;
&lt;select id=&quot;subject&quot; name=&quot;subject&quot;&gt;
&lt;option value=&quot;Advertising Inquiry&quot;&gt;Advertising Inquiry&lt;/option&gt;
&lt;option value=&quot;Support Inquiry&quot;&gt;Support Inquiry&lt;/option&gt;
&lt;option value=&quot;General Inquiry&quot;&gt;General Inquiry&lt;/option&gt;
&lt;/select&gt;
&lt;label for=&quot;message&quot;&gt;Message:&lt;/label&gt;
&lt;textarea id=&quot;message&quot; name=&quot;message&quot; rows=&quot;&quot; cols=&quot;&quot;&gt;&lt;/textarea&gt;
&lt;input type=&quot;submit&quot; id=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /&gt;
</pre>
<p>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>
<pre class="brush: jscript; title: ; notranslate">
&lt;script type=&quot;text/javascript&quot;&gt;
&lt;!--//
function validateForm(){
var emailfilter =/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var phonefilter =/\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
if(document.getElementById(&quot;name&quot;).value==&quot;&quot;){
document.getElementById(&quot;error_msg&quot;).childNodes[0].data =
&quot;Please enter full name&quot;;
document.getElementById(&quot;error_msg&quot;).style.display = &quot;block&quot;;
document.getElementById(&quot;name&quot;).focus();
return false;
}
&lt;/script&gt;
</pre>
<p>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>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.dingobytes.com @ 2012-02-05 21:05:50 -->
