<?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>Dalton Filho &#187; Javascript</title>
	<atom:link href="http://www.daltonfilho.com/category/programming/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daltonfilho.com</link>
	<description></description>
	<lastBuildDate>Sun, 13 Sep 2009 03:41:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Javascript time validation explained</title>
		<link>http://www.daltonfilho.com/2008/01/20/javascript-time-validation-explained/</link>
		<comments>http://www.daltonfilho.com/2008/01/20/javascript-time-validation-explained/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 00:38:29 +0000</pubDate>
		<dc:creator>Dalton Filho</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[validation]]></category>
		<category><![CDATA[web programming]]></category>

		<guid isPermaLink="false">http://www.daltonfilho.com/2008/01/20/javascript-time-validation-explained/</guid>
		<description><![CDATA[Regular Expressions are one of the most overlooked things in programming. A simple search on Google for form validation returns many cook-book style tutorials that help making things worse by just showing an ugly finished expression (or an even uglier string parsing function). Because it's so easy to shy away from regular expressions just by seeing them finished, I've decided to demonstrate a regular expression construction step-by-step. The expression will validate an <code>hh:mm:ss</code> time pattern.]]></description>
			<content:encoded><![CDATA[<style type="text/css"> <!--  button {               height: 32px;  }  .centered-code { 	text-align: center; 	width: 100%; 	background-color:#FFFFCC; 	font-family:"Courier New", Courier, mono; 	font-size: small; } -->  </style>
<p>Regular Expressions are one of the most overlooked things in programming. A simple search on Google for form validation returns many cook-book style tutorials that help making things worse by just showing an ugly finished expression (or an even uglier string parsing function). Because it&#8217;s so easy to shy away from regular expressions just by seeing them finished, I&#8217;ve decided to demonstrate a regular expression construction step-by-step. The expression will validate an <code>hh:mm:ss</code> time pattern. Here it is:</p>
<p class="centered-code">^((\d)|(0\d)|(1\d)|(2[0-3]))\:((\d)|([0-5]\d))\:((\d)|([0-5]\d))$</p>
<p>On a first glance it looks ugly, but if you consider that it validates that time pattern in a single line, it starts to look better. Let&#8217;s proceed to the step-by-step construction.</p>
<p>First, let&#8217;s start with the body of the expression. You don&#8217;t want to validate anything before or after the expression itself, so you delimitate it with <code>^</code> (to match the beginning of string) and <code>$</code> (to match the end of the string):</p>
<p class="centered-code">^$</p>
<p>This results in an expression that will match only an empty string. Try it now:</p>
<p align="left">
<input id="test-1" type="text" /> <button onclick="validateField('test-1','^$')">Validate</button> Pattern: <code>^$</code></p>
<p>If you just used <code>^</code> or <code>$</code>, the expression would match anything that had a start or an end, respectively, which is not what you want. Try it now:</p>
<p align="left">
<input id="test-2" type="text" /> <button onclick="validateField('test-2','^')">Validate</button> Pattern: <code>^</code></p>
<p align="left">
<input id="test-3" type="text" /> <button onclick="validateField('test-3','$')">Validate</button> Pattern: <code>$</code></p>
<p>When we create regular expressions, we should be able to think in terms of expression cases. The first case is the hour value. It can be any value from 00 to 23, and we must consider the single digit cases too (0 to 9). Let&#8217;s create an expression for each case and test them. The first case is <code>0-9</code>:</p>
<p>The user may choose to type just a single digit, so we must be prepared for this case. A regular expression that can match is <code>\d</code>, which stands for &#8220;any digit&#8221;. The <code>^\d$</code> expression matches any string that contains only a single digit:</p>
<p align="left">
<input id="test-4" type="text" /> <button onclick="validateField('test-4','^\\d$')">Validate</button> Pattern: <code>^\d$</code></p>
<p>Besides <code>0-9</code> numbers, the user can also enter a two-digit number such as 18, but we must delimitate this range from 00 to 23. Because regular expressions work on a character basis (and not on a number basis), we must think in terms of which characters can vary and how they can vary. You can&#8217;t, for instance, use <code>\d\d</code>, because that would allow numbers like 45 to match. Neither can you use <code>[0-2][0-3]</code> because that would not allow numbers like 19 to match. It&#8217;s time to subdivide them into groups, which are the following:</p>
<ul>
<li><code>0\d</code> &#8211; allows numbers from 00 to 09 to match</li>
<li><code>1\d</code> &#8211; allows numbers from 10 to 19 to match</li>
<li><code>2[0-3]</code> &#8211; allows numbers from 20 to 23 to match</li>
</ul>
<p>When creating regular expressions it&#8217;s nice to test them one by one to be sure they&#8217;re working:</p>
<p align="left">
<input id="test-5" type="text" /> <button onclick="validateField('test-5','^0\\d$')">Validate</button> Pattern: <code>^0\d$</code></p>
<p align="left">
<input id="test-6" type="text" /> <button onclick="validateField('test-6','^1\\d$')">Validate</button> Pattern: <code>^1\d$</code></p>
<p align="left">
<input id="test-7" type="text" /> <button onclick="validateField('test-7','^2[0-3]$')">Validate</button> Pattern: <code>^2[0-3]$</code></p>
<p>Now we can validade any hour value by just grouping all cases so far. Grouping is very intuitive because it uses <code>(...)</code> to group and <code>|</code> to alternate between groups:</p>
<p class="centered-code">^((\d)|(0\d)|(1\d)|(2[0-3]))$</p>
<p align="left">
<input id="test-8" type="text" /> <button onclick="validateField('test-8','^((\\d)|(0\\d)|(1\\d)|(2[0-3]))$')">Validate</button> Pattern: <code>^((\d)|(0\d)|(1\d)|(2[0-3]))$</code></p>
<p>Time to include the <code>:</code> sign, but because it has a regexp meaning, you should precede it with <code>\</code> on the expression such as this:</p>
<p align="left">
<input id="test-8" type="text" /> <button onclick="validateField('test-8','^\\:$')">Validate</button> Pattern: <code>^\:$</code></p>
<p>Moving on to the minute and second values, the digits range from 00 to 59, and we should include the single digit value as well. The minutes case is much easier because we can give a lot of freedom for the second digit. The <code>[0-5]\d</code> expression does the trick:</p>
<p align="left">
<input id="test-9" type="text" /> <button onclick="validateField('test-9','^[0-5]\\d$')">Validate</button> Pattern: <code>^[0-5]\d$</code></p>
<p>Joining this expression with the single digit rule yields the following expression:</p>
<p class="centered-code">^((\d)|([0-5]\d))$</p>
<p align="left">
<input id="test-10" type="text" /> <button onclick="validateField('test-10','^((\\d)|([0-5]\\d))$')">Validate</button> Pattern: <code>^((\d)|([0-5]\d))$</code></p>
<p>With this expression ready, the seconds value is ready as well, since the seconds rule is the same as the minutes rule. Putting it all together we have the final expression:</p>
<p class="centered-code">^((\d)|(0\d)|(1\d)|(2[0-3]))\:((\d)|([0-5]\d))\:((\d)|([0-5]\d))$</p>
<p align="left">
<input id="test-11" type="text" /> <button onclick="validateField('test-11','^((\\d)|(0\\d)|(1\\d)|(2[0-3]))\\:((\\d)|([0-5]\\d))\\:((\\d)|([0-5]\\d))$')">Validate</button> The final <code>hh:mm:ss</code> validation pattern</p>
<p><script type="text/javascript"> function validateField(id,pattern) {var value = document.getElementById(id).value; if (value.match(new RegExp(pattern))) {window.alert(value + " matches the " + pattern + " pattern"); } else {window.alert(value + " does not match the " + pattern + " pattern");}} </script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.daltonfilho.com/2008/01/20/javascript-time-validation-explained/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
