<?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>moreHawes &#187; PHP</title>
	<atom:link href="http://www.morehawes.co.uk/category/programming/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.morehawes.co.uk</link>
	<description></description>
	<lastBuildDate>Thu, 29 Jul 2010 17:47:59 +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>Easily Generate Javascript and PHP Error Checking For Your Forms</title>
		<link>http://www.morehawes.co.uk/easily-generate-javascript-and-php-error-checking-your-forms</link>
		<comments>http://www.morehawes.co.uk/easily-generate-javascript-and-php-error-checking-your-forms#comments</comments>
		<pubDate>Fri, 10 Apr 2009 19:17:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Javacsript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.morehawes.co.uk/cms/easily-generate-javascript-and-php-error-checking-your-forms</guid>
		<description><![CDATA[<p>I wanted a quick and easy way of adding Javascript and PHP error checking to my form elements. The benefit of this is that errors can be caught using Javascript before the form is submitted, but if JS is disabled in the users browser then the error is still caught using PHP.</p>

<p>The method I came up with to solve this is very simple and can be easily added to form elements with little extra code. The error checking is done by checking input against regular expressions.</p>]]></description>
			<content:encoded><![CDATA[<p>I wanted a quick and easy way of adding Javascript and PHP error checking to my form elements. The benefit of this is that errors can be caught using Javascript before the form is submitted, but if JS is disabled in the users browser then the error is still caught using PHP.</p>
<p>The method I came up with to solve this is very simple and can be easily added to form elements with little extra code. The error checking is done by checking input against regular expressions.</p>
<p>See the <a href="/stuff/error_checking.php">demo</a> and <a href="/stuff/error_checking.txt">demo source code</a>.</p>
<h3>Code Explaination</h3>
<p>One function is used to create both forms of error checking:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$jsChecks</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//holds JS error checks</span>
<span style="color: #000088;">$errorFound</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">//PHP error flag</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Creates both JS and PHP error checks</span>
<span style="color: #000000; font-weight: bold;">function</span> errorCheck<span style="color: #009900;">&#40;</span><span style="color: #000088;">$elementName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$regExp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errMsg</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$jsChecks</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$errorFound</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//Create Javascript error checking</span>
	<span style="color: #000088;">$check</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
	var '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$elementName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'Filter = '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$regExp</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'; 
	if (!'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$elementName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'Filter.test(theForm.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$elementName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.value)) { 
		errorMessage += &quot;\n  * '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$errMsg</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;;
		errorFound = true;
	}'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$jsChecks</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$check</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//PHP error checking and return input class</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;formSubmitted&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	<span style="color: #666666; font-style: italic;">//if form submitted</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regExp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$elementName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$errorFound</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//css error class</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">//no class if no error detected</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Error checking is created for the form elements specifying which element (by name), a regular expression to be checked against and a custom error message:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Create error checking</span>
<span style="color: #000088;">$nameError</span> <span style="color: #339933;">=</span> errorCheck<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/([a-z0-9]{3}[a-z0-9]?)/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Name is too short&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">//Must be 3 chars or over</span>
<span style="color: #000088;">$emailError</span> <span style="color: #339933;">=</span> errorCheck<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;email&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;/^[a-z0-9]+([_<span style="color: #000099; font-weight: bold;">\\</span>.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+<span style="color: #000099; font-weight: bold;">\\</span>.[a-z]{2,}$/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Invalid Email Address&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">//must be valid email address</span></pre></div></div>

<p>If no error is found then process the data.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//If no error found</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$errorFound</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Process form data...</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>In our head tags we add the following Javascript and CSS rules. The Javascipt checks we created for each element are included here by the PHP ehco:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
input {
	border: 1px solid #DDD;
}
.error {
	border: 1px dashed #F00;
}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">function validateForm(theForm) {</span>
<span style="color: #808080; font-style: italic;">	var errorFound = false;</span>
<span style="color: #808080; font-style: italic;">	var errorMessage = &quot;Please correct the following errors:\n&quot;;</span>
<span style="color: #808080; font-style: italic;">	&lt;?php echo $jsChecks ?&gt;</span>
<span style="color: #808080; font-style: italic;">	if(errorFound) {</span>
<span style="color: #808080; font-style: italic;">		alert(errorMessage);</span>
<span style="color: #808080; font-style: italic;">	}</span>
<span style="color: #808080; font-style: italic;">	return !errorFound;</span>
<span style="color: #808080; font-style: italic;">}</span>
<span style="color: #808080; font-style: italic;">//--&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>The form is really simple with code to display a CSS class depending on whether a error is detected (for the PHP error checking) and to set the value of the field:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">onSubmit</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;return validateForm(this)&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;name&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>Name<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> (must be 3 characters or over)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;name&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php echo $nameError ?&gt;</span></span>&quot; value=&quot;<span style="color: #009900;">&lt;?php echo @ $_POST<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span> ?&gt;</span>&quot; /&gt;<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>Email Address<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span> (must be a valid email address)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;30&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php echo $emailError ?&gt;</span></span>&quot; value=&quot;<span style="color: #009900;">&lt;?php echo @ $_POST<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'email'</span><span style="color: #66cc66;">&#93;</span> ?&gt;</span>&quot; /&gt;
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;formSubmitted&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;formSubmitted&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

<p>There you are, both Javascript and PHP error checking! See the complete source code <a href="/stuff/error_checking.txt">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.morehawes.co.uk/easily-generate-javascript-and-php-error-checking-your-forms/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
