<?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>adc&#124;works &#187; Uncategorized</title>
	<atom:link href="http://www.adcworks.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adcworks.com</link>
	<description></description>
	<lastBuildDate>Wed, 06 Jan 2010 15:47:08 +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>Code Igniter Date Validation</title>
		<link>http://www.adcworks.com/2009/04/code-igniter-date-validation/</link>
		<comments>http://www.adcworks.com/2009/04/code-igniter-date-validation/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 13:39:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code Igniter]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.adcworks.com/?p=38</guid>
		<description><![CDATA[Those looking for a Code Igniter date validator rule can use the following custom validation. This is written for CI 1.7+ To use, copy the class below into your system/application/libraries folder named MY_Form_validation.php, then in your controller use it like any other rule. Oh, and this is the first version so if you do come]]></description>
			<content:encoded><![CDATA[<p>Those looking for a Code Igniter date validator rule can use the following custom validation. This is written for CI 1.7+</p>
<p>To use, copy the class below into your system/application/libraries folder named MY_Form_validation.php, then in your controller use it like any other rule.</p>
<p>Oh, and this is the first version so if you do come across bugs/requests let me know would you? <img src='http://www.adcworks.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Cheers.</p>
<h3>Form</h3>
<p>This validation rule validates an input text box only right now.</p>
<pre class="brush: php">
&lt;input type=&quot;text&quot; name=&quot;date&quot; value=&quot;&lt;?php echo set_value(&#039;date&#039;); ?&gt;&quot; size=&quot;10&quot; /&gt;
</pre>
<h3>Controller Usage</h3>
<p>UK</p>
<pre class="brush: php">
$this-&gt;form_validation-&gt;set_rules(&#039;date&#039;, &#039;date&#039;, &#039;trim|required|valid_date[d/m/y,/]&#039;);
</pre>
<p>US</p>
<pre class="brush: php">
$this-&gt;form_validation-&gt;set_rules(&#039;date&#039;, &#039;date&#039;, &#039;trim|required|valid_date[m/d/y,/]&#039;);
</pre>
<p>Database</p>
<pre class="brush: php">
$this-&gt;form_validation-&gt;set_rules(&#039;date&#039;, &#039;date&#039;, &#039;trim|required|valid_date[y-m-d,-]&#039;);
</pre>
<h3>The Code</h3>
<pre class="brush: php">
&lt;?php if (!defined(&#039;BASEPATH&#039;)) exit(&#039;No direct script access allowed&#039;);

class MY_Form_validation extends CI_Form_validation {

    function MY_Form_validation()
    {
        parent::CI_Form_validation();
    }

    /**
    * @desc Validates a date format
    * @params format,delimiter
    * e.g. d/m/y,/ or y-m-d,-
    */
    function valid_date($str, $params)
    {
        // setup
        $CI =&amp;amp;amp;amp;amp;amp; get_instance();
        $params = explode(&#039;,&#039;, $params);
        $delimiter = $params[1];
        $date_parts = explode($delimiter, $params[0]);

        // get the index (0, 1 or 2) for each part
        $di = $this-&gt;valid_date_part_index($date_parts, &#039;d&#039;);
        $mi = $this-&gt;valid_date_part_index($date_parts, &#039;m&#039;);
        $yi = $this-&gt;valid_date_part_index($date_parts, &#039;y&#039;);

        // regex setup
        $dre = &quot;(0?1|0?2|0?3|0?4|0?5|0?6|0?7|0?8|0?9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)&quot;;
        $mre = &quot;(0?1|0?2|0?3|0?4|0?5|0?6|0?7|0?8|0?9|10|11|12)&quot;;
        $yre = &quot;([0-9]{4})&quot;;
        $red = &#039;\\&#039;.$delimiter; // escape delimiter for regex
        $rex = &quot;^[0]{$red}[1]{$red}[2]$&quot;;

        // do replacements at correct positions
        $rex = str_replace(&quot;[{$di}]&quot;, $dre, $rex);
        $rex = str_replace(&quot;[{$mi}]&quot;, $mre, $rex);
        $rex = str_replace(&quot;[{$yi}]&quot;, $yre, $rex);

        if (ereg($rex, $str, $matches)) {
            // skip 0 as it contains full match, check the date is logically valid
            if (checkdate($matches[$mi + 1], $matches[$di + 1], $matches[$yi + 1])) {
                return true;
            } else {
                // match but logically invalid
                $CI-&gt;form_validation-&gt;set_message(&#039;valid_date&#039;, &quot;The date is invalid.&quot;);
                return false;
            }
        } 

        // no match
        $CI-&gt;form_validation-&gt;set_message(&#039;valid_date&#039;, &quot;The date format is invalid. Use {$params[0]}&quot;);
        return false;
    }      

    function valid_date_part_index($parts, $search) {
        for ($i = 0; $i &lt;= count($parts); $i++) {
            if ($parts[$i] == $search) {
                return $i;
            }
        }
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.adcworks.com/2009/04/code-igniter-date-validation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
