<?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>Interactive IQ : The Hessler Design Blog &#187; validation</title>
	<atom:link href="http://www.hesslerdesign.com/blog/tag/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hesslerdesign.com/blog</link>
	<description>Smart Developers = Smart Websites</description>
	<lastBuildDate>Thu, 26 Jan 2012 23:00:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Bulletproof Your Online Forms for Mobile Devices with PHP – Part 2</title>
		<link>http://www.hesslerdesign.com/blog/tutorials/bulletproof-your-online-forms-for-mobile-devices-with-php-%e2%80%93-part-2/</link>
		<comments>http://www.hesslerdesign.com/blog/tutorials/bulletproof-your-online-forms-for-mobile-devices-with-php-%e2%80%93-part-2/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 16:50:54 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[websites]]></category>

		<guid isPermaLink="false">http://www.hesslerdesign.com/blog/?p=566</guid>
		<description><![CDATA[In the last post, we explored how to set up an online form using HTML and PHP. In this post, we&#8217;re going to finish setting up the logic to store form field values and validate the form.  If you haven&#8217;t read the last post and want to catch up, please click here to go to [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://www.hesslerdesign.com/blog/tutorials/bulletproof-your-online-forms-for-mobile-devices-with-php-part-1/">last post</a>, we explored how to set up an online form using HTML and PHP.  In this post, we&#8217;re going to finish setting up the logic to store form field values and validate the form.  If you haven&#8217;t read the last post and want to catch up, please <a href="http://www.hesslerdesign.com/blog/tutorials/bulletproof-your-online-forms-for-mobile-devices-with-php-part-1/" target="_self">click here to go to part 1</a>.</p>
<h4>Step 1: Add PHP Functions for Setting/Resetting Variables</h4>
<p>In the last post, we set the form up so the value for each form field is set to a corresponding PHP variable. Now we need to make sure that when the form field changes and a user action (like submitting the form) takes place, the PHP variables get set to the newly updated form field values.  This is a simple process, as we just have to create a function to set the PHP variables.  And while we&#8217;re at it, we can create another function to reset the PHP variables for our &#8216;Clear Form&#8217; button.  Here are the functions we&#8217;ll use.  Note that we need to use the &#8216;global&#8217; keyword inside the functions to access and successfully update the variables.</p>
<p><code><span style="color: #0000ff;">function</span> setVariables<span style="color: #000099;">() {</span><br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_name;<br />
&nbsp; &nbsp;$user_name <span style="color: #0000ff;">=</span> <span style="color: #0066ff;">$_POST</span><span style="color: #000099;">[</span><span style="color: #cc0000;">"user_name"</span><span style="color: #000099;">]</span>;<br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_email;<br />
&nbsp; &nbsp;$user_email <span style="color: #0000ff;">=</span> <span style="color: #0066ff;">$_POST</span><span style="color: #000099;">[</span><span style="color: #cc0000;">"user_email"</span><span style="color: #000099;">]</span>;<br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_message;<br />
&nbsp; &nbsp;$user_message <span style="color: #0000ff;">=</span> <span style="color: #0066ff;">$_POST</span><span style="color: #000099;">[</span><span style="color: #cc0000;">"user_message"</span><span style="color: #000099;">]</span>;<br />
<span style="color: #000099;">}</span><br />
<span style="color: #0000ff;">function</span> resetVariables<span style="color: #000099;">() {</span><br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_name;<br />
&nbsp; &nbsp;$user_name <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">""</span>;<br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_email;<br />
&nbsp; &nbsp;$user_email <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">""</span>;<br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_message;<br />
&nbsp; &nbsp;$user_message <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">""</span>;<br />
<span style="color: #000099;">}</span></code></p>
<p>Again, these functions will eventually get called when a user clicks on the Submit or Reset button, respectively.</p>
<p><span id="more-566"></span></p>
<h4>Step 2: Add PHP Functions for Form Field Validation</h4>
<p>Now it&#8217;s time to write out the validation functions for the form.  In our example, we have three fields.  The name and message fields will be a simple character count for their validation.  In the email validation, we will use one of the well known regular expressions that will check the email value for correct syntax.</p>
<p>For the simple text input, we&#8217;ll create a function that accepts two arguments.  The first argument will be the value to get validated, and the second will be the minimum number of characters that need to be present in the first value.  Inside the function, there will be some simple conditional logic that will check the string length of the first value to the minimum number of characters.  We&#8217;ll also set a return value of &#8216;y&#8217; or &#8216;n&#8217; so we&#8217;ll know whether or not our value is valid.  Here&#8217;s what our simple text input validation function will look like:</p>
<p><code><span style="color: #0000ff;">function</span> validateTextInput<span style="color: #000099;">(</span>$str, $minLength<span style="color: #0000ff;">=</span><span style="color: #ff0000;">1</span><span style="color: #000099;">) {</span><br />
&nbsp; &nbsp;<span style="color: #006600;">if</span> <span style="color: #000099;">(</span><span style="color: #0000ff;">strlen</span><span style="color: #000099;">(</span>$str<span style="color: #000099;">)</span> <span style="color: #0000ff;">&lt;</span> $minLength<span style="color: #000099;">) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;$lValid <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">'n'</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span> <span style="color: #006600;">else</span> <span style="color: #000099;">{</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;$lValid <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">'y'</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span><br />
&nbsp; &nbsp;<span style="color: #006600;">return</span> $lValid;<br />
<span style="color: #000099;">}</span></code></p>
<p>For the email validation function, we&#8217;ll simply pass in the value to check as the one and only argument.  Inside the function, the conditional logic checks the value against the aforementioned regular expression.  And, like the text input validation function above, this one also returns a &#8216;y&#8217; or &#8216;n&#8217; value telling us whether or not the value is valid.  Here&#8217;s what our email validation function will look like:</p>
<p><code><span style="color: #0000ff;">function</span> validateEmail<span style="color: #000099;">(</span>$str<span style="color: #000099;">) {</span><br />
&nbsp; &nbsp;<span style="color: #006600;">if</span> <span style="color: #000099;">(</span><span style="color: #0000ff;">!eregi</span><span style="color: #000099;">(</span><span style="color: #cc0000;">"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"</span>, $str<span style="color: #000099;">)) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;$lValid <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">'n'</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span> <span style="color: #006600;">else</span> <span style="color: #000099;">{</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;$lValid <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">'y'</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span><br />
&nbsp; &nbsp;<span style="color: #006600;">return</span> $lValid;<br />
<span style="color: #000099;">}</span></code></p>
<h4>Step 3: Add PHP Function for Overall Form Validation</h4>
<p>Now that we have set up the individual form field validation functions, we&#8217;ll need a master function that will be called when the user clicks the Submit button.  It will validate all of our fields, and will hold logic to either show an error or send the form.  Here&#8217;s what the function will look like.  An explanation of the function is below the function.</p>
<p><code><span style="color: #0000ff;">function</span> validateForm<span style="color: #000099;">() {</span><br />
&nbsp; &nbsp;$default_error <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">"There was a problem with your entry. Please correct the following fields."</span>;<br />
&nbsp; &nbsp;$error <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">""</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #999999;">// Set up global variables to check</span><br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_name;<br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_email;<br />
&nbsp; &nbsp;<span style="color: #006600;">global</span> $user_message;<br />
<br />
&nbsp; &nbsp;<span style="color: #999999;">// Perform validation for fields</span><br />
&nbsp; &nbsp;$valid_user_name <span style="color: #0000ff;">=</span> validateTextInput<span style="color: #000099;">(</span>$user_name, <span style="color: #ff0000;">1</span><span style="color: #000099;">)</span>;<br />
&nbsp; &nbsp;$valid_user_email <span style="color: #0000ff;">=</span> validateEmail<span style="color: #000099;">(</span>$user_email<span style="color: #000099;">)</span>;<br />
&nbsp; &nbsp;$valid_user_message <span style="color: #0000ff;">=</span> validateTextInput<span style="color: #000099;">(</span>$user_message, <span style="color: #ff0000;">5</span><span style="color: #000099;">)</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #006600;">if</span> <span style="color: #000099;">(</span>$valid_user_name <span style="color: #0000ff;">!=</span> <span style="color: #cc0000;">'y'</span><span style="color: #000099;">) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;$error <span style="color: #0000ff;">.=</span> <span style="color: #cc0000;">"&lt;li&gt;Your Name&lt;/li&gt;"</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span><br />
&nbsp; &nbsp;<span style="color: #006600;">if</span> <span style="color: #000099;">(</span>$valid_user_email <span style="color: #0000ff;">!=</span> <span style="color: #cc0000;">'y'</span><span style="color: #000099;">) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;$error <span style="color: #0000ff;">.=</span> <span style="color: #cc0000;">"&lt;li&gt;Your Email&lt;/li&gt;"</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span><br />
&nbsp; &nbsp;<span style="color: #006600;">if</span> <span style="color: #000099;">(</span>$valid_user_message <span style="color: #0000ff;">!=</span> <span style="color: #cc0000;">'y'</span><span style="color: #000099;">) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;$error <span style="color: #0000ff;">.=</span> <span style="color: #cc0000;">"&lt;li&gt;Message&lt;/li&gt;"</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span><br />
<br />
&nbsp; &nbsp;<span style="color: #999999;">// Return error value or send form</span><br />
&nbsp; &nbsp;<span style="color: #006600;">if</span> <span style="color: #000099;">(</span>$error<span style="color: #000099;">) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;div class=\"error\"&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;label&gt;"</span> . $default_error . <span style="color: #cc0000;">"&lt;/label&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;ul&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> $error;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;/ul&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;/div&gt;"</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span> <span style="color: #006600;">else</span> <span style="color: #000099;">{</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;sendForm<span style="color: #000099;">()</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span><br />
<span style="color: #000099;">}</span></code></p>
<p>First, the function will have two variables: <code>$default_error</code> and <code>$error</code>.  As you noticed, the <code>$default_error</code> has the general text calling out that there&#8217;s an error, and the <code>$error</code> variable is blank by default.</p>
<p>Next, we set up the global variables that we need to check.</p>
<p>Third, we set up a unique variable that will be set to the return value of our individual field validation functions.  After these variables are set up, we put in conditional logic that appends the <code>$error</code> variable with <code>&lt;li&gt;</code> tags if the variable isn&#8217;t set to &#8216;y&#8217;.</p>
<p>Finally, there is conditional logic that checks the <code>$error</code> variable.  If this variable is set to anything other than its default blank value, we echo out some HTML markup that writes out our <code>$default_error</code> message, and then lists out each individual form field that is invalid.  If the variable is still blank, we call the <code>sendForm()</code> function (not included in this article &#8211; you can write that out depending on the needs of your project).</p>
<h4>Step 4: Add Markup and Logic for Submit and Reset Button Actions</h4>
<p>Now that we have everything set up, we obviously need to be able to call our validation and reset functions, based on what button the user clicks.  I am a fan of putting this logic inside of a <code>&lt;div&gt;</code> tag at the top of the form.  Since the validation function echos out a message if invalid, it will thus echo out to a div tag that we can style however we want.  Same with the reset logic, as we will first display a &#8220;are you sure you want to reset?&#8221; message with a &#8216;Yes&#8217; and &#8216;No&#8217; button.  Here&#8217;s the markup we&#8217;ll insert for this logic, that we&#8217;ll place between the opening <code>&lt;form&gt;</code> tag and the first <code>&lt;label&gt;</code>.</p>
<p><code><span style="color: #999999;">&lt;!-- Error Message --&gt;</span><br />
<span style="color: #000099;">&lt;div id=</span><span style="color: #0000ff;">"error-message"</span><span style="color: #000099;">&gt;</span><br />
&nbsp; &nbsp;<span style="color: #ff0000;">&lt;?php</span><br />
&nbsp; &nbsp;<span style="color: #006600;">if</span> <span style="color: #000099;">(</span><span style="color: #0066ff;">$_POST</span><span style="color: #000099;">[</span><span style="color: #cc0000;">"submit"</span><span style="color: #000099;">]) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;setVariables<span style="color: #000099;">()</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;validateForm<span style="color: #000099;">()</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span><br />
&nbsp; &nbsp;<span style="color: #ff0000;">?&gt;</span><br />
<span style="color: #000099;">&lt;/div&gt;</span></code></p>
<p><code><span style="color: #999999;">&lt;!-- Reset Confirmation Message --&gt;</span><br />
<span style="color: #000099;">&lt;div id=</span><span style="color: #0000ff;">"confirm-reset"</span><span style="color: #000099;">&gt;</span><br />
&nbsp; &nbsp;<span style="color: #ff0000;">&lt;?php</span><br />
&nbsp; &nbsp;<span style="color: #006600;">if</span> <span style="color: #000099;">(</span><span style="color: #0000ff;">isset</span><span style="color: #000099;">(</span><span style="color: #0066ff;">$_POST</span><span style="color: #000099;">[</span><span style="color: #cc0000;">'reset'</span><span style="color: #000099;">])) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;setVariables<span style="color: #000099;">()</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;div class=\"confirm\"&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;label&gt;Are you sure you want to reset?&lt;/label&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;div class=\"hidden\"&gt;&lt;br /&gt;&lt;/div&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;input type=\"submit\" class=\"button\" name=\"reset-yes\" value=\"Yes\" /&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;div style=\"display:inline; width:16px;\"&gt;&amp;nbsp;&amp;nbsp;&lt;/div&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;input type=\"submit\" class=\"button\" name=\"reset-no\" value=\"No\" /&gt;"</span>;<br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #0000ff;">echo</span> <span style="color: #cc0000;">"&lt;/div&gt;"</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span> <span style="color: #006600;">else if</span> <span style="color: #000099;">(</span><span style="color: #0000ff;">isset</span><span style="color: #000099;">(</span><span style="color: #0066ff;">$_POST</span><span style="color: #000099;">[</span><span style="color: #cc0000;">'reset-no'</span><span style="color: #000099;">])) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;setVariables<span style="color: #000099;">()</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span> <span style="color: #006600;">else if</span> <span style="color: #000099;">(</span><span style="color: #0000ff;">isset</span><span style="color: #000099;">(</span><span style="color: #0066ff;">$_POST</span><span style="color: #000099;">[</span><span style="color: #cc0000;">'reset-yes'</span><span style="color: #000099;">])) {</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;resetVariables<span style="color: #000099;">()</span>;<br />
&nbsp; &nbsp;<span style="color: #000099;">}</span><br />
&nbsp; &nbsp;<span style="color: #ff0000;">?&gt;</span><br />
<span style="color: #000099;">&lt;/div&gt;</span></code></p>
<p>As you see, the error message <code>&lt;div&gt;</code> tag has conditional logic that checks for <code><span style="color: #0066ff;">$_POST</span>[<span style="color: #cc0000;">"submit"</span>]</code>.  If our submit button is clicked, the two functions inside this conditional statement are fired off.  First, we call the <code>setVariables()</code> function, which as outlined above, sets all of our global PHP variables to the value of the form fields.  After we have called this function, we call our <code>validateForm()</code> function that validates each field and checks whether to display an error message or send the form.</p>
<p>The reset <code>&lt;div&gt;</code> tag is a bit more detailed.  Our form &#8216;Reset&#8217; button is set up with name &#8216;reset&#8217;, which is equivalent to <code><span style="color: #0066ff;">$_POST</span>[<span style="color: #cc0000;">'reset'</span>]</code>.  This is the first check of our conditional logic here, which calls the <code>setVariables()</code>, and echos out a <code>&lt;div&gt;</code> tag with the reset confirmation message and accompanying &#8216;Yes&#8217; and &#8216;No&#8217; buttons.</p>
<p>The second and third checks of the reset conditional logic check for the &#8216;Yes&#8217; and &#8216;No&#8217; button clicks.  If the &#8216;No&#8217; button is clicked, we simply call the <code>setVariables()</code> function.  If the &#8216;Yes&#8217; button is clicked, we call the <code>resetVariables()</code> function which as outlined above, resets all variables and therefore erases the form field values.</p>
<h4>Wrapping It Up</h4>
<p>As I mentioned at the beginning of the article, this is useful because all of the logic is done with PHP, and can thus be used on any browser and device, regardless of how the settings are configured.  What I like to do is to put all of the logic in its own file, and then include it in the page wherever I need it.  That way, if I have a completely separate set of files for full web vs. mobile, for instance, I can still use the same one file that contains the form, and style it with CSS depending on what version of the site the user is seeing.</p>
<p>Also, at this point, feel free to add in your own CSS and/or other HTML markup for how the form is set up and will be displayed.</p>
<p>Also, since there&#8217;s A LOT of code and logic here, I have gone ahead and packaged up the files in a zip file that you can download for your convenience.  The zip file includes some sample CSS styles that I have set up to customize the look of the form.</p>
<p>Until next time, happy coding!</p>
<p><a title="Download Source Files" href="http://www.hesslerdesign.com/blog/wp-content/uploads/2010/08/hd_php_form.zip"><img class="alignleft size-full wp-image-572" title="Download Source Files" src="http://www.hesslerdesign.com/blog/wp-content/uploads/2010/08/btn_downloadSourceFiles.png" alt="" width="176" height="29" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hesslerdesign.com/blog/tutorials/bulletproof-your-online-forms-for-mobile-devices-with-php-%e2%80%93-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bulletproof Your Online Forms for Mobile Devices with PHP &#8211; Part 1</title>
		<link>http://www.hesslerdesign.com/blog/tutorials/bulletproof-your-online-forms-for-mobile-devices-with-php-part-1/</link>
		<comments>http://www.hesslerdesign.com/blog/tutorials/bulletproof-your-online-forms-for-mobile-devices-with-php-part-1/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 16:09:28 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[websites]]></category>

		<guid isPermaLink="false">http://www.hesslerdesign.com/blog/?p=541</guid>
		<description><![CDATA[The more popular mobile devices get, the more reason there is to be cautious and deliberate when creating your website. While there are a bevy of issues in dealing with creating mobile-friendly sites, the one I’d like to focus on here is building out an HTML form. More specifically, since some mobile devices don’t have [...]]]></description>
			<content:encoded><![CDATA[<p>The more popular mobile devices get, the more reason there is to be cautious and deliberate when creating your website. While there are a bevy of issues in dealing with creating mobile-friendly sites, the one I’d like to focus on here is building out an HTML form. More specifically, since some mobile devices don’t have JavaScript enabled (either by default or by choosing, either way done to increase speed on your device), it’s not necessarily good to rely on JavaScript validation for your forms. And, since most current mobile devices aren’t Flash-friendly either (thank you Steve Jobs), the best method in creating a form is using HTML, styled with CSS, and using PHP for validation and processing.</p>
<p>With that said, here’s part 1 of how to build a bulletproof form using only HTML, PHP and CSS.</p>
<h4>Step 1: Plan Your Form</h4>
<p>This first step is important when building out a form.  Rather than just jumping in and starting to throw together a form, it&#8217;s helpful (and a big time-saver) to plan out what fields your form will have, and what types of fields they will be.  I find it helpful to either jot this info down on a piece of paper, or write it in pseudo-code inside of a comment block in my working file.  Either way, plan your form fields first.  It will make life much easier as you build out the form, and eventually its validation and processing.</p>
<p>For this tutorial, we&#8217;ll build out a form with three fields: Name, Email, and Message.  The form will also have a Submit and Reset button for form processing and resetting.</p>
<h4>Step 2: Create PHP Variables for Each Form Field Value</h4>
<p>Now that we have the form architecture figured out, we need to assign a PHP variable to each form field.  In our example, this means we’ll need 3 variables.  So at the very top of our file, inside of the PHP wrapper tags (<strong><span style="color: #ff0000;"><code>&lt;?php ?&gt;</code></span></strong>), create a variable for each.  In our example, it will look like this:</p>
<p><code><strong><span style="color: #ff0000;">&lt;?php</span></strong><br />
$user_name <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">""</span>;<br />
$user_email <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">""</span>;<br />
$user_message <span style="color: #0000ff;">=</span> <span style="color: #cc0000;">""</span>;<br />
<strong><span style="color: #ff0000;">?&gt;</span></strong></code></p>
<p><em>Note: This step could be swapped with step #3 below, but for the sake of  brevity in this tutorial, I&#8217;m putting it here so we can plug these  variable values directly in the form fields when we build out the form  in step #3.</em></p>
<p><span id="more-541"></span></p>
<h4>Step 3: Build Out the Form</h4>
<p>This step is simple, as we just need to put together the pure HTML code for our form.  Our form will have two <code>input</code> fields and one <code>textarea</code> (for the message).  While assembling the code, I&#8217;m going to insert some extra <code>&lt;div&gt;</code> and <code>&lt;span&gt;</code> tags that are inserted to break up the form into containers in the absence of CSS.  This is done as a precaution for mobile devices that are not set up to enable CSS.  Many times the default is okay, but I&#8217;ve noticed that here and there, you need to hammer it down a little bit.</p>
<p>Another important thing that we&#8217;ll do here is to set the value of each form field to its accompanying PHP variable.  This is a setup step that will ensure that our form will &#8220;remember&#8221; its values, should any of the form fields fail in the validation process (still to come).  You know what I mean here – we’ve all been there – you fill out a long form, submit, and find out that the form is somehow invalid, and all of the form fields are blank again, forcing you to refill the entire thing all over again.  Frustrating, isn’t it?</p>
<p>This is actually much easier than you might think using PHP.  For the <code>input</code> fields, it’s a simple matter of echoing out the PHP variable inside of the ‘value’ attribute of the tag.  For the <code>textarea</code>, you’ll need to echo out the PHP variable between the opening and closing tags.  And don’t worry – since our PHP variables are set as empty strings by default, the fields will also be empty when the form initially loads.  But as the form and validation logic get built out, our PHP variables are set to match the form fields, and thus the form fields will be populated with the &#8220;remembered&#8221; value of the field (stored in the variables) rather than being blank.</p>
<p>In our example, our form looks a little something like this:</p>
<p><code><span style="color: #ff9900;">&lt;form action=</span><span style="color: #0000ff;">""</span> <span style="color: #ff9900;">method=</span><span style="color: #0000ff;">"post"</span> <span style="color: #ff9900;">name=</span><span style="color: #0000ff;">"contact_form"</span> <span style="color: #ff9900;">id=</span><span style="color: #0000ff;">"contact_form"</span><span style="color: #ff9900;">&gt;</span><br />
&nbsp; &nbsp;<span style="color: #000099;">&lt;div&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #ff9900;">&lt;label for=</span><span style="color: #0000ff;">"user_name"</span><span style="color: #ff9900;">&gt;</span>Your Name:<span style="color: #ff9900;">&lt;/label&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #000099;">&lt;span class=</span><span style="color: #0000ff;">"hidden"</span><span style="color: #000099;">&gt;&lt;br /&gt;&lt;/span&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #ff9900;">&lt;input type=</span><span style="color: #0000ff;">"text"</span> <span style="color: #ff9900;">name=</span><span style="color: #0000ff;">"user_name"</span> <span style="color: #ff9900;">value=</span><span style="color: #0000ff;">"</span><span style="color: #ff0000;">&lt;?php</span> <span style="color: #0000ff;">echo</span> $user_name; <span style="color: #ff0000;">?&gt;</span><span style="color: #0000ff;">"</span> <span style="color: #ff9900;">autocomplete=</span><span style="color: #0000ff;">"off"</span> <span style="color: #ff9900;">tabindex=</span><span style="color: #0000ff;">"1"</span> <span style="color: #ff9900;">/&gt;</span><br />
&nbsp; &nbsp;<span style="color: #000099;">&lt;/div&gt;</span><br />
&nbsp; &nbsp;<span style="color: #000099;">&lt;div&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #ff9900;">&lt;label for=</span><span style="color: #0000ff;">"user_email"</span><span style="color: #ff9900;">&gt;</span>Your Email:<span style="color: #ff9900;">&lt;/label&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #000099;">&lt;span class=</span><span style="color: #0000ff;">"hidden"</span><span style="color: #000099;">&gt;&lt;br /&gt;&lt;/span&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #ff9900;">&lt;input type=</span><span style="color: #0000ff;">"text"</span> <span style="color: #ff9900;">name=</span><span style="color: #0000ff;">"user_email"</span> <span style="color: #ff9900;">value=</span><span style="color: #0000ff;">"</span><span style="color: #ff0000;">&lt;?php</span> <span style="color: #0000ff;">echo</span> $user_email; <span style="color: #ff0000;">?&gt;</span><span style="color: #0000ff;">"</span> <span style="color: #ff9900;">autocomplete=</span><span style="color: #0000ff;">"off"</span> <span style="color: #ff9900;">tabindex=</span><span style="color: #0000ff;">"2"</span> <span style="color: #ff9900;">/&gt;</span><br />
&nbsp; &nbsp;<span style="color: #000099;">&lt;/div&gt;</span><br />
&nbsp; &nbsp;<span style="color: #000099;">&lt;div&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #ff9900;">&lt;label for=</span><span style="color: #0000ff;">"user_message"</span><span style="color: #ff9900;">&gt;</span>Message:<span style="color: #ff9900;">&lt;/label&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #000099;">&lt;span class=</span><span style="color: #0000ff;">"hidden"</span><span style="color: #000099;">&gt;&lt;br /&gt;&lt;/span&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #ff9900;">&lt;textarea name=</span><span style="color: #0000ff;">"user_message"</span> <span style="color: #ff9900;">autocomplete=</span><span style="color: #0000ff;">"off"</span> <span style="color: #ff9900;">wrap=</span><span style="color: #0000ff;">"physical"</span> <span style="color: #ff9900;">tabindex=</span><span style="color: #0000ff;">"3"</span><span style="color: #ff9900;">&gt;</span><span style="color: #ff0000;">&lt;?php</span> <span style="color: #0000ff;">echo</span> $user_message; <span style="color: #ff0000;">?&gt;</span><span style="color: #ff9900;">&lt;/textarea&gt;</span><br />
&nbsp; &nbsp;<span style="color: #000099;">&lt;/div&gt;</span><br />
&nbsp; &nbsp;<span style="color: #000099;">&lt;div class=</span><span style="color: #0000ff;">"centered"</span><span style="color: #000099;">&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #ff9900;">&lt;input type=</span><span style="color: #0000ff;">"submit"</span> <span style="color: #ff9900;">name=</span><span style="color: #0000ff;">"submit"</span> <span style="color: #ff9900;">value=</span><span style="color: #0000ff;">"Submit &amp;raquo;"</span> <span style="color: #ff9900;">/&gt;</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;<span style="color: #ff9900;">&lt;input type=</span><span style="color: #0000ff;">"submit"</span> <span style="color: #ff9900;">name=</span><span style="color: #0000ff;">"reset"</span> <span style="color: #ff9900;">value=</span><span style="color: #0000ff;">"Reset Form"</span> <span style="color: #ff9900;">/&gt;</span><br />
&nbsp; &nbsp;<span style="color: #000099;">&lt;/div&gt;</span><br />
<span style="color: #ff9900;">&lt;/form&gt;</span></code></p>
<p>A few important notes about the form setup:</p>
<ul>
<li>We didn&#8217;t include any value in the action attribute for the form tag.  This is because we&#8217;re going to house all of the form logic within the same file to reduce page-to-page navigation, and the amount of external files we need to concern ourselves with.  There are different schools of thought about this, but this is the method we&#8217;ll use for this tutorial.</li>
<li>As discussed above, PHP markup has been inserted into the form fields as their values.</li>
<li>Each form field tag has a unique name.  This will be necessary when we start to set/reset variables and validate the form later on down the road.</li>
<li>Each form field is set up here with the autocomplete attribute set to &#8220;off&#8221;. This is just more of a security precaution that I like on contact forms, that makes it so the browser doesn&#8217;t remember previous values you&#8217;ve typed in other forms, in the case that you&#8217;re using a public computer that doesn&#8217;t delete private browsing history.  <em>(Note: This doesn&#8217;t have anything to do with the method we&#8217;re using to echo out our PHP variables into the form, as our PHP variable method will simply keep our values in the form upon unsuccessful form submission, whereas the autocomplete attribute would have a dropdown of &#8220;remembered&#8221; values of past forms when the user starts typing.)</em></li>
<li>Overall, I&#8217;ve included a number of ids and classes in the form that will be later used to style it with CSS for browsers and mobile devices that have CSS enabled.</li>
</ul>
<h4>What&#8217;s Next</h4>
<p>We now have the framework for our form, and have set up some of the initial PHP logic to help us populate and store form values.  In the next segment, we&#8217;ll work on adding in the variable setting/resetting functionality, as well as the validation functions.  Also, and most importantly, we&#8217;ll tie in the Submit and Reset buttons with the correct functionality to make the form do what it&#8217;s supposed to do.</p>
<p>Until the next part of this tutorial, happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hesslerdesign.com/blog/tutorials/bulletproof-your-online-forms-for-mobile-devices-with-php-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a Better Email Validator in Flash</title>
		<link>http://www.hesslerdesign.com/blog/flash/building-a-better-email-validator-in-flash/</link>
		<comments>http://www.hesslerdesign.com/blog/flash/building-a-better-email-validator-in-flash/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 21:55:25 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[actionscript 3]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.hesslerdesign.com/blog/?p=147</guid>
		<description><![CDATA[I work on a lot of Flash projects that require form validation. One issue that keeps coming up is the process of field validation, and more specifically, email validation. There are numerous rules that people use to validate email addresses (does it contain an @ sign, does it contain a dot, how many characters are [...]]]></description>
			<content:encoded><![CDATA[<p>I work on a lot of Flash projects that require form validation. One issue that keeps coming up is the process of field validation, and more specifically, email validation. There are numerous rules that people use to validate email addresses (does it contain an @ sign, does it contain a dot, how many characters are there, etc.), but there is a fine line between overly strict validation and validation that&#8217;s way too loose. So I did some investigating about what makes up an email address, and how to best validate an email address in Flash. The result of my work is a robust EmailValidator class that I authored, written in ActionScript 3.0. <span id="more-147"></span></p>
<p>&nbsp;</p>
<h4>The Validation Logic</h4>
<p>Here&#8217;s a quick rundown of what this EmailValidator class validates:</p>
<ul>
<li>Must contain three parts: [1] Local part, [2] @ sign, [3] Domain</li>
<li>Local part must contain at least one of the following valid characters:
<ul>
<li>Upper and lowercase letters (A-Z, a-z)</li>
<li>Numeric Digits (0-9)</li>
<li>Other characters:  ! # $ % &amp; &#8216; * + &#8211; / = ? ^ _ ` { | } ~</li>
<li>Periods (provided it is not the first or last character, and does not appear more than two times consecutively)</li>
</ul>
</li>
<li>Domain must contain at least one period, as well as at least one character before the period as well as a valid top-level domain (i.e. com, org, ca, uk) after the last period.  Valid domain characters include:
<ul>
<li>Upper and lowercase English letters (A-Z, a-z)</li>
<li>Numeric Digits (0-9)</li>
<li>Hyphens</li>
<li>Periods (provided it is not the first or last character, and does not appear more than two times consecutively)</li>
</ul>
</li>
</ul>
<p>With that list of validation logic written, I must also confess that, technically speaking, quoted strings and spaces (so long as they are within quotes) are valid in the local-part of an email. However, on the Internet Engineering Task Force (IETF) website, <a href="http://tools.ietf.org/html/rfc5321" target="_blank">RFC 5321 Section 4.1.2</a> contends that,</p>
<blockquote><p><em>&#8220;&#8230;a host that expects to receive mail SHOULD avoid defining mailboxes where the Local-part requires (or uses) the Quoted-string form&#8230;&#8221;</em></p></blockquote>
<p>Since the practice of using quoted strings is discouraged by RFC 5321, the logic in my EmailValidator class does NOT allow quoted strings, including the double-quote character and spaces.</p>
<p>&nbsp;</p>
<h4>An Example</h4>
<p>I have included a small Flash email validator file below that you can test the validity of an inputted email address.  It utilizes the EmailValidator class that I have written.  Feel free to test out a range of email addresses.  Note that the validator gives useful feedback about invalid addresses, specifying what the first encountered issue is when checking whether or not the address valid.</p>
<p>&nbsp;</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="414" height="108" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0"><param name="src" value="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/09/email.swf" /><embed type="application/x-shockwave-flash" width="414" height="108" src="http://www.hesslerdesign.com/blog/wp-content/uploads/2009/09/email.swf"></embed></object></p>
<p>&nbsp;</p>
<h4>Known Issues &#038; Exceptions:</h4>
<ol>
<li>Many email providers out there will only accept letters, digits and hyphens as valid in the local part of the email. Though this is becoming more common, the EmailValidator class will still allow any of the valid characters listed above, even if it results in an invalid email address for a specific email client (i.e. */+@gmail.com). This is done to follow the direction of RFC 5322, and the list of valid atom characters listed in Section 3.2.3 that can be included in the local part of an email. Until this list is deprecated (if ever), it&#8217;s an exception I&#8217;m willing to live with.</li>
<li>Though the EmailValidator checks for a valid domain (including only alpha characters, numbers and hyphens, as well as valid top-level domain), there&#8217;s no way to tell whether the domain actually exists. (For example, me@fake.net will validate, though www.fake.net may not be a valid site/domain.)</li>
</ol>
<p>&nbsp;</p>
<h4>Resources:</h4>
<p><a href="http://www.ietf.org/rfc.html" target="_blank">Internet Engineering Task Force (IETF)</a><br />
<a href="http://www.iana.org/domains/root/db/" target="_blank">Internet Assigned Numbers Authority (IANA)</a></p>
<p>&nbsp;</p>
<p>Interested in getting this EmailValidator class for your own use? <a href="http://www.hesslerdesign.com/#/contact">Contact me</a> to request a copy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hesslerdesign.com/blog/flash/building-a-better-email-validator-in-flash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

