Commits

Phil Sturgeon committed a01b6bf

Added decimal, less_than and greater_than rules to the Form validation Class.

Comments (0)

Files changed (3)

system/libraries/Form_validation.php

 
 		// Build our master array
 		$this->_field_data[$field] = array(
-											'field'				=> $field,
-											'label'				=> $label,
-											'rules'				=> $rules,
-											'is_array'			=> $is_array,
-											'keys'				=> $indexes,
-											'postdata'			=> NULL,
-											'error'				=> ''
-											);
+			'field'				=> $field,
+			'label'				=> $label,
+			'rules'				=> $rules,
+			'is_array'			=> $is_array,
+			'keys'				=> $indexes,
+			'postdata'			=> NULL,
+			'error'				=> ''
+		);
 
 		return $this;
 	}
 	 */
 	function integer($str)
 	{
-		return (bool)preg_match( '/^[\-+]?[0-9]+$/', $str);
+		return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
+	}
+
+	// --------------------------------------------------------------------
+
+	/**
+	 * Decimal number
+	 *
+	 * @access	public
+	 * @param	string
+	 * @return	bool
+	 */
+	function decimal($str)
+	{
+		return (bool) preg_match('/^[\-+]?[0-9]+\.[0-9]+$/', $str);
+	}
+
+	// --------------------------------------------------------------------
+
+	/**
+	 * Greather than
+	 *
+	 * @access	public
+	 * @param	string
+	 * @return	bool
+	 */
+	function greater_than($str, $min)
+	{
+		if ( ! is_numeric($str))
+		{
+			return false;
+		}
+		return $str > $min;
+	}
+
+	// --------------------------------------------------------------------
+
+	/**
+	 * Less than
+	 *
+	 * @access	public
+	 * @param	string
+	 * @return	bool
+	 */
+	function less_than($str, $max)
+	{
+		if ( ! is_numeric($str))
+		{
+			return false;
+		}
+		return $str < $max;
 	}
 
 	// --------------------------------------------------------------------
 	 */
 	function is_natural($str)
 	{
-		return (bool)preg_match( '/^[0-9]+$/', $str);
+		return (bool) preg_match( '/^[0-9]+$/', $str);
 	}
 
 	// --------------------------------------------------------------------

user_guide/changelog.html

 <p>Release Date: n/a<br />
 Hg Tag: n/a</p>
 
+<ul>
+	<li>Libraries
+		<ul>
+			<li class="reactor">Added <kbd>decimal</kbd>, <kbd>less_than</kbd> and <kbd>greater_than</kbd> rules to the <a href="libraries/parser.html">Form validation Class</a>.</li>
+		</ul>
+	</li>
+</ul>
+
 <h3>Bug fixes for 2.0.1</h3>
 <ul>
 	<li class="reactor">CLI requests can now be run from any folder, not just when CD'ed next to index.php.</li>

user_guide/libraries/form_validation.html

 <p>The following is a list of all the native rules that are available to use:</p>
 
 
+<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
+	<tr>
+		<th>Rule</th>
+		<th>Parameter</th>
+		<th>Description</th>
+		<th>Example</th>
+	</tr>
 
-<table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
-<tr>
-<th>Rule</th>
-<th>Parameter</th>
-<th>Description</th>
-<th>Example</th>
-</tr><tr>
+	<tr>
+		<td class="td"><strong>required</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element is empty.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<td class="td"><strong>required</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element is empty.</td>
-<td class="td">&nbsp;</td>
-</tr><tr>
+	<tr>
+		<td class="td"><strong>matches</strong></td>
+		<td class="td">Yes</td>
+		<td class="td">Returns FALSE if the form element does not match the one in the parameter.</td>
+		<td class="td">matches[form_item]</td>
+	</tr>
 
-<td class="td"><strong>matches</strong></td>
-<td class="td">Yes</td>
-<td class="td">Returns FALSE if the form element does not match the one in the parameter.</td>
-<td class="td">matches[form_item]</td>
-</tr><tr>
+	<tr>
+		<td class="td"><strong>min_length</strong></td>
+		<td class="td">Yes</td>
+		<td class="td">Returns FALSE if the form element is shorter then the parameter value.</td>
+		<td class="td">min_length[6]</td>
+	</tr>
 
-<td class="td"><strong>min_length</strong></td>
-<td class="td">Yes</td>
-<td class="td">Returns FALSE if the form element is shorter then the parameter value.</td>
-<td class="td">min_length[6]</td>
-</tr><tr>
+	<tr>
+		<td class="td"><strong>max_length</strong></td>
+		<td class="td">Yes</td>
+		<td class="td">Returns FALSE if the form element is longer then the parameter value.</td>
+		<td class="td">max_length[12]</td>
+	</tr>
 
-<td class="td"><strong>max_length</strong></td>
-<td class="td">Yes</td>
-<td class="td">Returns FALSE if the form element is longer then the parameter value.</td>
-<td class="td">max_length[12]</td>
-</tr><tr>
+	<tr>
+		<td class="td"><strong>exact_length</strong></td>
+		<td class="td">Yes</td>
+		<td class="td">Returns FALSE if the form element is not exactly the parameter value.</td>
+		<td class="td">exact_length[8]</td>
+	</tr>
 
-<td class="td"><strong>exact_length</strong></td>
-<td class="td">Yes</td>
-<td class="td">Returns FALSE if the form element is not exactly the parameter value.</td>
-<td class="td">exact_length[8]</td>
-</tr><tr>
+	<tr>
+		<td class="td"><strong>greater_than</strong></td>
+		<td class="td">Yes</td>
+		<td class="td">Returns FALSE if the form element is less than the parameter value or not numeric.</td>
+		<td class="td">greater_than[8]</td>
+	</tr>
 
-<td class="td"><strong>alpha</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element contains anything other than alphabetical characters.</td>
-<td class="td">&nbsp;</td>
-</tr><tr>
+	<tr>
+		<td class="td"><strong>less_than</strong></td>
+		<td class="td">Yes</td>
+		<td class="td">Returns FALSE if the form element is greater than the parameter value or not numeric.</td>
+		<td class="td">less_than[8]</td>
+	</tr>
 
-<td class="td"><strong>alpha_numeric</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element contains anything other than alpha-numeric characters.</td>
-<td class="td">&nbsp;</td>
-</tr><tr>
+	<tr>
+		<td class="td"><strong>alpha</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element contains anything other than alphabetical characters.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<td class="td"><strong>alpha_dash</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>alpha_numeric</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element contains anything other than alpha-numeric characters.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<tr>
-<td class="td"><strong>numeric</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element contains anything other than numeric characters.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>alpha_dash</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element contains anything other than alpha-numeric characters, underscores or dashes.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<tr>
-<td class="td"><strong>integer</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element contains anything other than an integer.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>numeric</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element contains anything other than numeric characters.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<tr>
-<td class="td"><strong>is_natural</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>integer</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element contains anything other than an integer.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<tr>
-<td class="td"><strong>is_natural_no_zero</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>decimal</strong></td>
+		<td class="td">Yes</td>
+		<td class="td">Returns FALSE if the form element is not exactly the parameter value.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<tr>
-<td class="td"><strong>valid_email</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the form element does not contain a valid email address.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>is_natural</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element contains anything other than a natural number: 0, 1, 2, 3, etc.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<tr>
-<td class="td"><strong>valid_emails</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if any value provided in a comma separated list is not a valid email.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>is_natural_no_zero</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element contains anything other than a natural number, but not zero: 1, 2, 3, etc.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<tr>
-<td class="td"><strong>valid_ip</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the supplied IP is not valid.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>valid_email</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the form element does not contain a valid email address.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
-<tr>
-<td class="td"><strong>valid_base64</strong></td>
-<td class="td">No</td>
-<td class="td">Returns FALSE if the supplied string contains anything other than valid Base64 characters.</td>
-<td class="td">&nbsp;</td>
-</tr>
+	<tr>
+		<td class="td"><strong>valid_emails</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if any value provided in a comma separated list is not a valid email.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
+
+	<tr>
+		<td class="td"><strong>valid_ip</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the supplied IP is not valid.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
+
+	<tr>
+		<td class="td"><strong>valid_base64</strong></td>
+		<td class="td">No</td>
+		<td class="td">Returns FALSE if the supplied string contains anything other than valid Base64 characters.</td>
+		<td class="td">&nbsp;</td>
+	</tr>
 
 
 </table>
 
 
 <table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder">
-<tr>
-<th>Name</th>
-<th>Parameter</th>
-<th>Description</th>
-</tr><tr>
+	<tr>
+		<th>Name</th>
+		<th>Parameter</th>
+		<th>Description</th>
+	</tr><tr>
 
-<td class="td"><strong>xss_clean</strong></td>
-<td class="td">No</td>
-<td class="td">Runs the data through the XSS filtering function, described in the <a href="input.html">Input Class</a> page.</td>
-</tr><tr>
+		<td class="td"><strong>xss_clean</strong></td>
+		<td class="td">No</td>
+		<td class="td">Runs the data through the XSS filtering function, described in the <a href="input.html">Input Class</a> page.</td>
+	</tr><tr>
 
-<td class="td"><strong>prep_for_form</strong></td>
-<td class="td">No</td>
-<td class="td">Converts special characters so that HTML data can be shown in a form field without breaking it.</td>
-</tr><tr>
+		<td class="td"><strong>prep_for_form</strong></td>
+		<td class="td">No</td>
+		<td class="td">Converts special characters so that HTML data can be shown in a form field without breaking it.</td>
+	</tr><tr>
 
-<td class="td"><strong>prep_url</strong></td>
-<td class="td">No</td>
-<td class="td">Adds "http://" to URLs if missing.</td>
-</tr><tr>
+		<td class="td"><strong>prep_url</strong></td>
+		<td class="td">No</td>
+		<td class="td">Adds "http://" to URLs if missing.</td>
+	</tr><tr>
 
-<td class="td"><strong>strip_image_tags</strong></td>
-<td class="td">No</td>
-<td class="td">Strips the HTML from image tags leaving the raw URL.</td>
-</tr><tr>
+		<td class="td"><strong>strip_image_tags</strong></td>
+		<td class="td">No</td>
+		<td class="td">Strips the HTML from image tags leaving the raw URL.</td>
+	</tr><tr>
 
-<td class="td"><strong>encode_php_tags</strong></td>
-<td class="td">No</td>
-<td class="td">Converts PHP tags to entities.</td>
-</tr>
+		<td class="td"><strong>encode_php_tags</strong></td>
+		<td class="td">No</td>
+		<td class="td">Converts PHP tags to entities.</td>
+	</tr>
 
 </table>