<?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>Krio Media &#187; JavaScript</title>
	<atom:link href="http://www.krio.me/category/development-info/javascript-tutorials-learn-js/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.krio.me</link>
	<description></description>
	<lastBuildDate>Mon, 05 Jul 2010 06:27:31 +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>JavaScript Form Validation Essentials Chapter 1</title>
		<link>http://www.krio.me/javascript-form-validation-essentials-chapter-1/</link>
		<comments>http://www.krio.me/javascript-form-validation-essentials-chapter-1/#comments</comments>
		<pubDate>Mon, 21 Dec 2009 03:58:57 +0000</pubDate>
		<dc:creator>Kevin Rio</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[form validation]]></category>
		<category><![CDATA[javascript turorials]]></category>

		<guid isPermaLink="false">http://kriomedia.com/?p=139</guid>
		<description><![CDATA[In this series we are going to learn how to validate input using JavaScript. We will not allow a user to submit a form without entering the appropriate information. This will help to greatly reduce the amount of spam that you receive, along with minimizing security risks. This is the first entry in a series [...]]]></description>
			<content:encoded><![CDATA[<p>In this series we are going to learn how to validate input using JavaScript. We will not allow a user to submit a form without entering the appropriate information. This will help to greatly reduce the amount of spam that you receive, along with minimizing security risks.</p>
<p><span id="more-139"></span>This is the first entry in a series that will cover many different types of input. First, we will make a form that can only be submitted when a user inputs a proper email address.</p>
<h1>The Form</h1>
<p>First things first, let build our HTML form.</p>
<pre class="brush: xml;">

&lt;form method=&quot;post&quot; action=&quot;&quot;&gt;

&lt;input id=&quot;email&quot; type=&quot;text&quot; name=&quot;email&quot;/&gt;

&lt;input type=&quot;submit&quot; name=&quot;emailSubmit&quot; onclick=&quot;return checkEmail();&quot;/&gt;

&lt;/form&gt;
</pre>
<p>Anything out of the ordinary here? First, you should notice the id=&#8221;email&#8221; in the email input box. Because of this id=&#8221;email&#8221; we are able to find out what the user has entered using JavaScript. Next,  in the submit input box, you should see onclick=&#8221;return checkEmail();&#8221;, which is what tells JavaScript to call our JavaScript function, which will validate the email address. &#8216;return&#8217; is used because if the function returns false, it will not submit the form, but if it returns true, the form will be submitted.</p>
<h1>The JavaScript</h1>
<p>Here is the JavaScript for the validation. This should be placed between the head tags of the HTML document.</p>
<pre class="brush: jscript;">

&lt;script type=&quot;text/javascript&quot;&gt;

 function checkEmail()
 {

 var email = document.getElementById('email').value;

 emailExpression = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;

 if(!email.match(emailExpression))
 {
 alert(&quot;Please enter a valid email!&quot;);
 return false;
 }

 return true;
 }

 &lt;/script&gt;
</pre>
<p>First we create the function checkEmail which will run when the submit button is pressed. Next we grab what the user has entered in the email box with the line: var email = document.getElementById(&#8216;email&#8217;).value; We store this value in the email variable. Next, we create our regular expression, which will be used to validate the email address.</p>
<p>You will then find an IF statement. This is used to actually compare the regular expression to the email variable, which we got from the form. If it does not match, an alert is placed on screen, and the form IS NOT submitted, however if it is an email address, the statement returns true and the form is submitted as normal.</p>
<p>I hope that this helps you begin to understand how you can validate forms on the client side using JavaScript. I will be expanding this series to include new types of input validation and ways of notifying the user.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.krio.me/javascript-form-validation-essentials-chapter-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
