<?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>Studge &#187; JavaScript</title>
	<atom:link href="http://studge.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://studge.com</link>
	<description></description>
	<lastBuildDate>Wed, 17 Sep 2008 17:21:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>JavaScript Validation for HTML Forms</title>
		<link>http://studge.com/javascript-validation-for-html-forms/</link>
		<comments>http://studge.com/javascript-validation-for-html-forms/#comments</comments>
		<pubDate>Tue, 10 Apr 2007 13:07:19 +0000</pubDate>
		<dc:creator>Studge</dc:creator>
				<category><![CDATA[(X)HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://studge.com/javascript-validation-for-html-forms/</guid>
		<description><![CDATA[One of the most frustrating things with using forms on a site is getting back bad information in your results. Probably the most common instance is when a user enters a bad email address, such as webmaster#studgecom.  We can utilize JavaScript to check for composition error such as this and to make sure that [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most frustrating things with using forms on a site is getting back bad information in your results.<span id="more-35"></span> Probably the most common instance is when a user enters a bad email address, such as <code>webmaster#studgecom</code>.  We can utilize JavaScript to check for composition error such as this and to make sure that certain, required fields are filled in.</p>
<p>First, we are going to write up a simple form that requests a user&#039;s name, email address and favorite color.  Here is the essence of the form:</p>
<div class="code">
<pre><code>&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Form for JavaScript Validation - 1st version&lt;/title&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;form method="POST" action="jstest.php"&gt;
    	Name:&lt;input type="text" name="name" /&gt;&lt;br /&gt;
	Email address:&lt;input type="text" name="email" /&gt;&lt;br /&gt;
	Favorite color:&lt;input type="text" name="color" /&gt;&lt;br /&gt;
	&lt;input type="submit" name="submit" value="Submit" /&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>
</div>
<p>You can try it <a href="http://studge.com/testbed/jstest1.html" target="_blank">here</a>. It sends the form data to a PHP script named <code>jstest.php</code>.  This script merely displays the information that the user put into the form.  Here is code for it:
<div class="code">
<pre><code>&lt;?php
  echo "Thank you <strong>" . $_POST['name'] . "</strong>&lt;br /&gt;";
  echo "We will contact you at <strong>" . $_POST['email'] .
    "</strong> regarding the color <strong>" . $_POST['color'] . "</strong>";
?&gt;</code></pre>
</div>
<p>As it is written now, the form provides no sort of validation and the user can submit anything or nothing in the availabe fields.  So we need to write a JavaScript script to handle the validation.  We will <strong>require</strong> the user to input their name, email address and favorite color.  The user must also input the email address in a valid format. We will call this file <code>jstest.js</code> and here is what it looks like:
<div class="code">
<pre><code>function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="") {
			alert(alerttxt);
			return false;
		}
		else {
			return true;
		}
	}
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos&lt;1||dotpos-apos&lt;2) {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_form(thisform) {
	with (thisform) {
		if (validate_required(name,"Please specify your name!")==false) {
			name.focus();
			return false;
		}
		if (validate_required(email,"Email must be filled out!")==false) {
			email.focus();
			return false;
		}
		if (validate_email(email,"Not a valid e-mail address!")==false) {
			email.focus();
			return false;
		}
		if (validate_required(color,"Forgot favorite color!")==false) {
			color.focus();
			return false;
		}
	}
}</code></pre>
</div>
<p>Now we have a script to validate the form, but it does nothing until we implement it into the form&#039;s code.  Here is the full form code (sans styling):
<div class="code">
<pre><code>&lt;html&gt;
&lt;head&gt;
  &lt;script type="text/javascript" src="jstest.js"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;form method="POST"
        onsubmit="return validate_form(this)" action="jstest.php"&gt;
    Name:&lt;input type="text" name="name" /&gt;&lt;br /&gt;
    Email address:&lt;input type="text" name="email" /&gt;&lt;br /&gt;
    Favorite color:&lt;input type="text" name="color" /&gt;&lt;br /&gt;
    &lt;input type="submit" name="Submit" value="Submit" /&gt;
  &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
</div>
<p>You can try the finished, with validation, <a href="http://studge.com/testbed/jstest2.html" target="_blank">here</a>.  You should be able to adapt the above code to your specific form needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://studge.com/javascript-validation-for-html-forms/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.293 seconds -->

