<?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>The Open Coder &#187; Web development</title>
	<atom:link href="http://www.opencoder.co.uk/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.opencoder.co.uk</link>
	<description>Helping the fellow geek</description>
	<lastBuildDate>Fri, 15 Apr 2011 12:25:41 +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>URL regular expression</title>
		<link>http://www.opencoder.co.uk/2011/01/url-regular-expression/</link>
		<comments>http://www.opencoder.co.uk/2011/01/url-regular-expression/#comments</comments>
		<pubDate>Wed, 05 Jan 2011 13:30:26 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Flex]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web development]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=461</guid>
		<description><![CDATA[This is just a quick post to share a regular expression for a URL I had to come up with when needing to validate a URL in a Flex app. The code below is for Flex, but would only require a few minor changes for another language, the double backslash before the ? appears to [...]]]></description>
			<content:encoded><![CDATA[<p>This is just a quick post to share a regular expression for a URL I had to come up with when needing to validate a URL in a Flex app. The code below is for Flex, but would only require a few minor changes for another language, the double backslash before the ? appears to be required for Flex, using a single backslash does not work, read more about that in this <a title="Flex regular expression issues" href="http://www.opencoder.co.uk/2010/03/regexpvalidator-issues/" target="_blank">older post</a>. This also contains what would be capturing brackets in other languages, I could have used non-capturing brackets but that would have made this already complicated example even more difficult to read.</p>
<div class="codesnip-container" >
<div class="actionscript codesnip" style="font-family:monospace;">linkValidator = <span class="kw2">new</span> RegExpValidator<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
linkValidator.<span class="me1">expression</span> = <span class="st0">&quot;(http(s)?:<span class="es0">\/</span><span class="es0">\/</span>)?(([a-z]+[a-z0-9<span class="es0">\-</span>]*[.])?([a-z0-9]+[a-z0-9<span class="es0">\-</span>]*[.])+[a-z]{2,3}|localhost)(<span class="es0">\/</span>[a-z0-9_-]+[a-z0-9_ -]*)*<span class="es0">\/</span>?(<span class="es0">\\</span>?[a-z0-9_-]+=[a-z0-9 ',.-]*(&amp;amp;[a-z0-9_-]+=[a-z0-9 ',.-]*)*)?(#[a-z0-9/_-]*)?$&quot;</span>;<br />
linkValidator.<span class="me1">noMatchError</span> = resourceManager.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&quot;lang&quot;</span>, <span class="st0">&quot;invalidURL&quot;</span><span class="br0">&#41;</span>;<br />
linkValidator.<span class="me1">flags</span> = <span class="st0">&quot;i&quot;</span>;<br />
linkValidator.<span class="me1">source</span> = linkTextArea;<br />
linkValidator.<span class="me1">property</span> = <span class="st0">&quot;text&quot;</span>;<br />
linkValidator.<span class="me1">trigger</span> = linkTextArea;<br />
linkValidator.<span class="me1">triggerEvent</span> = Event.<span class="me1">CHANGE</span>;</div>
</div>
<p>I&#8217;ll break it down to the individual sections with a brief explaination.</p>
<pre>
//protocol and subdomain
(http(s)?:\/\/)?(([a-z]+[a-z0-9\-]*[.])?
</pre>
<p>The first part includes the protocol (http:// or https://), I am only dealing with web http urls here and it is optional in my app hence the ? at the end of the first group, the rest includes an optional subdomain which should start with one or more letters followed by zero or more letters/numbers/hyphens and a dot. This first subdomain and dot is also optional. So far this would match: <em>[empty string] http:// https//www. https://ww2</em> etc.</p>
<pre>
//server hostname
([a-z0-9]+[a-z0-9\-]*[.])+[a-z]{2,3}|localhost)
</pre>
<p>This next part includes the rest of the web host, the first grouping (first enclosing brackets) specifies the start of the hostname or a further subdomain which must start with a letter or number followed by a dot (the dot as a character set is how to represent the dot in Flex, you might be able to just use <em>\.</em>). This can be repeated many times, but then should be followed by 2 or three characters. Alternatively the hostname localhost can be used instead, the extra closing bracket matches the additional opening one after the protocol. This section should match: <em>www.example.com localhost example.com example.co.uk co.uk</em> etc.</p>
<pre>
//web path
(\/[a-z0-9_-]+[a-z0-9_ -]*)*\/?
</pre>
<p>This next part consists of the optional path (directory from the web root), it starts with a forward slash and can be any number of letters, numbers, underscores, spaces or hyphens, but can not start with a space (you might need to backslash escape your hypen in a different language. The trailing backslash is also optional as is the entire path. This part should match: <em>[empty string] / /directory /a/b/</em> etc.</p>
<pre>
//query string
(\\?[a-z0-9_-]+=[a-z0-9 ',.-]*(&amp;amp;[a-z0-9_-]+=[a-z0-9 ',.-]*)*)?
</pre>
<p>This part contains the optional query string part of the URL. Starting with a ? (may require only a single backslash in a different language), followed by the first parameter made up of one or more letters/numbers/underscores/hyphens the equals sign, followed by an optional parameter value made up of letters/numbers/spaces/apostrophes/commas/dots/hypens. This parameter=value part of the query string can be repeated several times after that but each extra parameter should be preceded with the ampersand (you would normally use just the &#038; for this, but flex requires the encoded version). This section could match: <em>[empty string] ?a= ?a=bc ?a=b&#038;c=d&#038;e=f</em> etc.</p>
<pre>
//fragment
(#[a-z0-9/_-]*)?$
</pre>
<p>Finally the last part of the expression contains the optional url fragment (the part with a #). In my case I specified zero or more letters/numbers/forward slashes/underscores/hypens (flex does not require escaping the forward slash when it is included in a character set). Then the dollar sign specifies that there should not be anything else after this. This could match: <em>[empty string] # #value #a/b/c</em> etc.</p>
<p>I hope this is useful to those struggling to create their own URL regular expression matchers. Flex devs remember to double escape the ? for the query string part of the URL.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2011/01/url-regular-expression/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery remote form validation</title>
		<link>http://www.opencoder.co.uk/2010/07/jquery-remote-form-validation/</link>
		<comments>http://www.opencoder.co.uk/2010/07/jquery-remote-form-validation/#comments</comments>
		<pubDate>Sat, 24 Jul 2010 16:06:56 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Web development]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=260</guid>
		<description><![CDATA[Again another long time between posts, this one is partly a small tutorial and partly about a small issue I encountered and workaround. This post discusses form validation using a cool jQuery form validation plugin created by Jörn Zaefferer, which I highly recommend, you can download the plugin from here. The plugin allows you to [...]]]></description>
			<content:encoded><![CDATA[<p>Again another long time between posts, this one is partly a small tutorial and partly about a small issue I encountered and workaround. This post discusses form validation using a cool jQuery form validation plugin created by Jörn Zaefferer, which I highly recommend, you can download the plugin from <a title="jQuery plugin, form validation" href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">here</a>. The plugin allows you to quickly and easily validate your html forms using the power of jQuery, the basic principle is that you specify the rules for validation on the fields of your form and submitting the form will be disabled until all fields are valid, where validation rules are broken the user is informed with messages next to the invalid fields.  I wouldn&#8217;t be surprised if this plugin became part of jQuery directly since so many people are using it already.</p>
<p>Lets have a look at a quick simple example, lets say I have a form with 3 input fields, name, email and website, we want to have the name and email fields required, we want to check the email is valid and because we are storing these fields in a database with a character limit we need to check the fields are not too long. Below is the html form code and the javascript for the validation.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span><br />
$<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test&quot;</span><span class="br0">&#41;</span>.<span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rules<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fullname<span class="sy0">:</span> <span class="br0">&#123;</span> required<span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span> maxlength<span class="sy0">:</span> 64 <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> 32 <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; website<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> 32 <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></p>
<p>&lt;form id=&quot;subscribe_test&quot; action=&quot;save_subscriber.php&quot; method=&quot;POST&quot;&gt;<br />
&nbsp; &lt;table&gt;<br />
&nbsp; &nbsp; &lt;tbody&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;label for=&quot;fullname&quot;&gt;Full name: *&lt;/label&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input id=&quot;fullname&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;label for=&quot;email&quot;&gt;Email: *&lt;/label&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input id=&quot;email&quot; class=&quot;email required&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;label for=&quot;website&quot;&gt;Website:&lt;/label&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input id=&quot;website&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input type=&quot;submit&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &lt;/tbody&gt;<br />
&nbsp; &lt;/table&gt;<br />
&lt;/form&gt;</div>
</div>
<p>In the above example you can see that you can apply the validation rules both in javascript and also using the predefined rule names as the css class for the input. E.g. the email input field is using classes <em>email</em> and <em>required</em>, the validation plugin will look for any class names on the inputs and apply the validation rule if there is one. I could have defined the email rule solely in the script but I wanted to show that there is a quicker way using classes. The working example for the above code is below.</p>
<p><script type="text/javascript">
jQuery(function($) { $("#subscribe_test").validate({ rules: {
     fullname: { required: true, maxlength: 64 },
     email: { maxlength: 32 },
     website: { maxlength: 32 }
 } });
});
</script></p>
<form id="subscribe_test" action="save_subscriber.php" method="POST">
<table>
<tbody>
<tr>
<td><label for="fullname">Full name: *</label></td>
<td>
<input id="fullname" name="fullname" type="text" /></td>
</tr>
<tr>
<td><label for="email">Email: *</label></td>
<td>
<input id="email" class="email required" name="email" type="text" /></td>
</tr>
<tr>
<td><label for="website">Website:</label></td>
<td>
<input id="website" name="website" type="text" /></td>
</tr>
<tr>
<td>
<input type="submit" /></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<p>You can also do remote validation. For example, lets say in our example we did not want someone with the same email address to be subscribed twice. You can use a special validation rule <em>remote</em> which points to a script which should output true or false based on the form input value. We will also give a special message for when the email address is already in use. The changed script code is then:</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span><br />
&nbsp;$<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test2&quot;</span><span class="br0">&#41;</span>.<span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#123;</span> rules<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fullname<span class="sy0">:</span> <span class="br0">&#123;</span> required<span class="sy0">:</span> <span class="kw2">true</span><span class="sy0">,</span> maxlength<span class="sy0">:</span> <span class="nu0">64</span> <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;email<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> <span class="nu0">32</span><span class="sy0">,</span> remote<span class="sy0">:</span> <span class="st0">&quot;/check_subscribed.php&quot;</span> <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;website<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> <span class="nu0">32</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; messages<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; email<span class="sy0">:</span><span class="br0">&#123;</span> remote<span class="sy0">:</span> <span class="st0">&quot;Already subscribed&quot;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></div>
</div>
<p>The running example now will not allow you to enter the email <em>test@example.com</em>, for this example there is a script called <em>check_subscribed.php</em> which is hard coded to look for the POST email variable and return false if it equals <em>test@example.com</em>.</p>
<p><script type="text/javascript">
jQuery(function($){      
    $("#subscribe_test2").validate({ 
        rules: {
           fullname: { required: true, maxlength: 64 },
           email: { maxlength: 32, remote: "/check_subscribed.php" },
           website: { maxlength: 32 }
        },
        messages: {
           email: { remote: "Already subscribed"}
        }
     });
});
</script></p>
<form id="subscribe_test2" action="save_subscriber.php" method="POST">
<table>
<tbody>
<tr>
<td><label>Full name: *</label></td>
<td>
<input name="fullname" type="text" /></td>
</tr>
<tr>
<td><label>Email: *</label></td>
<td>
<input class="email required" name="email" type="text" /></td>
</tr>
<tr>
<td><label>Website:</label></td>
<td>
<input name="website" type="text" /></td>
</tr>
<tr>
<td>
<input type="submit" /></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<p>So that is all good everything seems to be validating fine. Now I will show you a small case to watch out for. Lets assume that the subscribe form needs to be submitted using ajax and only accepts a required email address which should be remotely validated. Below is the code and the working example, if you simply enter the already subscribed email <em>test@example.com</em> and submit you see it does not work properly, the form gets submitted even though the validation fails.
<p><strong>Update 29/09/2010</strong> since updating this post and rechecking the code it appears, this actually does work. Perhaps the browsers have changed the order the javascript events occur, I certainly did not change anything, this <em>was</em> an issue previously.</p>
<p>Code and example below.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;"><span class="sy0">&lt;</span>script type<span class="sy0">=</span><span class="st0">&quot;text/javascript&quot;</span><span class="sy0">&gt;</span><br />
jQuery<span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>$<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;$<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test3&quot;</span><span class="br0">&#41;</span>.<span class="me1">submit</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>$<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">valid</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$.<span class="me1">ajax</span><span class="br0">&#40;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;type<span class="sy0">:</span> <span class="st0">&quot;POST&quot;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url<span class="sy0">:</span> <span class="st0">&quot;/save_subscriber.php&quot;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;data<span class="sy0">:</span> <span class="st0">&quot;email=&quot;</span><span class="sy0">+</span>$<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test3_email&quot;</span><span class="br0">&#41;</span>.<span class="me1">val</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;success<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>data<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;#submit_result&quot;</span><span class="br0">&#41;</span>.<span class="me1">html</span><span class="br0">&#40;</span><span class="st0">&quot;Ajax submit results:<br />
&quot;</span> <span class="sy0">+</span> data<span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">false</span><span class="sy0">;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
&nbsp; &nbsp; $<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test3&quot;</span><span class="br0">&#41;</span>.<span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#123;</span> rules<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;email<span class="sy0">:</span> <span class="br0">&#123;</span> maxlength<span class="sy0">:</span> <span class="nu0">32</span><span class="sy0">,</span> remote<span class="sy0">:</span> <span class="st0">&quot;/check_subscribed.php&quot;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="sy0">,</span> messages<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; email<span class="sy0">:</span> <span class="br0">&#123;</span> remote<span class="sy0">:</span> <span class="st0">&quot;Already subscribed&quot;</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></p>
<p>&lt;form id=&quot;subscribe_test3&quot; action=&quot;save_subscriber.php&quot; method=&quot;POST&quot;&gt;<br />
&nbsp; &lt;table&gt;<br />
&nbsp; &nbsp; &lt;tbody&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;label&gt;Email: *&lt;/label&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input id=&quot;subscribe_test3_email&quot; class=&quot;email required&quot; name=&quot;email&quot; type=&quot;text&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;tr&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input type=&quot;submit&quot; /&gt;&lt;/td&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;/tr&gt;<br />
&nbsp; &nbsp; &lt;/tbody&gt;<br />
&nbsp; &lt;/table&gt;<br />
&lt;/form&gt;</div>
</div>
<p><script type="text/javascript">
jQuery(function($){
       $("#subscribe_test3").submit(function(e){
          if ($(this).valid()){
               $.ajax( {
                   type: "POST",
                   url: "/save_subscriber.php",
                   data: "email="+$("#subscribe_test3_email").val(),
                   success: function(data){
                        $("#submit_result").html("Ajax submit results:" + data);
                   }
               });
          }
          return false;
    });
    $("#subscribe_test3").validate({ rules: {
         email: { maxlength: 32, remote: "/check_subscribed.php" }
     }, messages: {
        email: { remote: "Already subscribed"}
     }
     });
});
</script></p>
<form id="subscribe_test3" action="save_subscriber.php" method="POST">
<table>
<tbody>
<tr>
<td><label>Email: *</label></td>
<td>
<input id="subscribe_test3_email" class="email required" name="email" type="text" /></td>
</tr>
<tr>
<td>
<input type="submit" /></td>
</tr>
</tbody>
</table>
</form>
<p>The reason the above example did not work properly is because the remote validation checking is done asynchronously, as is the ajax request. In the above code, the form submit event binding function checks first if the form is valid before doing the ajax submit. The trouble is that the remote validation is done asynchronously. So the valid check is just returning true instead of false. In order to solve this the remote validation needs to be done synchronously instead (or async: false). Below is the relevant fix and the fixed example.</p>
<div class="codesnip-container" >
<div class="javascript codesnip" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">&quot;#subscribe_test3&quot;</span><span class="br0">&#41;</span>.<span class="me1">validate</span><span class="br0">&#40;</span><span class="br0">&#123;</span><br />
&nbsp; rules<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; email<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; maxlength<span class="sy0">:</span> <span class="nu0">32</span><span class="sy0">,</span> remote<span class="sy0">:</span> <span class="br0">&#123;</span> url<span class="sy0">:</span> <span class="st0">&quot;/check_subscribed.php&quot;</span><span class="sy0">,</span> async<span class="sy0">:</span> <span class="kw2">false</span> <span class="br0">&#125;</span> <br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><span class="sy0">,</span><br />
&nbsp; messages<span class="sy0">:</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; email<span class="sy0">:</span> <span class="br0">&#123;</span> remote<span class="sy0">:</span> <span class="st0">&quot;Already subscribed&quot;</span><span class="br0">&#125;</span><br />
&nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></div>
</div>
<p><script type="text/javascript">
jQuery(function($){
       $("#subscribe_test4").submit(function(e){
          if ($(this).valid()){
               $.ajax( {
                   type: "POST",
                   url: "save_subscriber.php",
                   data: "email="+$("#subscribe_test4_email").val(),
                   success: function(data){
                        $("#submit_result2").html("Ajax submit results:" + data);
                   }
               });
          }
          return false;
    });
    $("#subscribe_test4").validate({ rules: {
         email: { maxlength: 32, remote: { url: "/check_subscribed.php", async: false } }
     }, messages: {
        email: { remote: "Already subscribed"}
     }
     });
});
</script></p>
<form id="subscribe_test4" action="save_subscriber.php" method="POST">
<table>
<tbody>
<tr>
<td><label>Email: *</label></td>
<td>
<input id="subscribe_test4_email" class="email required" name="email" type="text" /></td>
</tr>
<tr>
<td>
<input type="submit" /></td>
</tr>
</tbody>
</table>
</form>
<p><strong>Update 27/09/2010</strong>
<p>It has been asked what the check_subscribed.php script did, so here it is below:</p>
<div class="codesnip-container" >
<div class="php codesnip" style="font-family:monospace;"><span class="kw2">&lt;?php</span><br />
<span class="re0">$email</span> <span class="sy0">=</span> <span class="re0">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&quot;email&quot;</span><span class="br0">&#93;</span><span class="sy0">;</span><br />
<span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$email</span> <span class="sy0">==</span> <span class="st0">&quot;test@example.com&quot;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">echo</span> <span class="st0">&quot;false&quot;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">echo</span> <span class="st0">&quot;true&quot;</span><span class="sy0">;</span><br />
<span class="br0">&#125;</span><br />
<span class="sy1">?&gt;</span></div>
</div>
<p><strong>Update 01/11/2010</strong>
<p>Someone has asked for the files used in these examples, so here is a <a href="http://www.opencoder.co.uk/wp-content/uploads/2011/03/remote_form_validation.zip">zip file</a> containing a html page and the necessary php scripts. The jquery and validation plugin are not included in the zip but are referenced as external scripts. This is a recommended way of including jQuery in your sites (but obviously a more up to date version).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2010/07/jquery-remote-form-validation/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

