diff options
author | Phil Sturgeon <email@philsturgeon.co.uk> | 2011-02-07 14:01:47 +0100 |
---|---|---|
committer | Phil Sturgeon <email@philsturgeon.co.uk> | 2011-02-07 14:01:47 +0100 |
commit | ef112c0830df4a31563351125888b0d522a1c965 (patch) | |
tree | 3552458b818c72d48af88784c59e0dd04a9d7a00 | |
parent | 287763b919d2f1522cf4e6e39c0d33a957ff0538 (diff) |
Added decimal, less_than and greater_than rules to the Form validation Class.
-rw-r--r-- | system/libraries/Form_validation.php | 70 | ||||
-rw-r--r-- | user_guide/changelog.html | 8 | ||||
-rw-r--r-- | user_guide/libraries/form_validation.html | 310 |
3 files changed, 237 insertions, 151 deletions
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index fc5b82ee3..745fb7c03 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -138,14 +138,14 @@ class CI_Form_validation { // 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; } @@ -1147,7 +1147,57 @@ class CI_Form_validation { */ 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; } // -------------------------------------------------------------------- @@ -1161,7 +1211,7 @@ class CI_Form_validation { */ function is_natural($str) { - return (bool)preg_match( '/^[0-9]+$/', $str); + return (bool) preg_match( '/^[0-9]+$/', $str); } // -------------------------------------------------------------------- diff --git a/user_guide/changelog.html b/user_guide/changelog.html index 09a2cefce..cd728226b 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -63,6 +63,14 @@ Change Log <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> diff --git a/user_guide/libraries/form_validation.html b/user_guide/libraries/form_validation.html index 935dca61f..d6120054b 100644 --- a/user_guide/libraries/form_validation.html +++ b/user_guide/libraries/form_validation.html @@ -924,118 +924,146 @@ POST array:</p> <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><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"> </td> -</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><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> - -<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> - -<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> - -<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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </td> -</tr> + <tr> + <th>Rule</th> + <th>Parameter</th> + <th>Description</th> + <th>Example</th> + </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"> </td> + </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> + + <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> + <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> + <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> + <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> + + <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> + + <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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </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"> </td> + </tr> </table> @@ -1058,36 +1086,36 @@ POST array:</p> <table cellpadding="0" cellspacing="1" border="0" style="width:100%" class="tableborder"> -<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>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>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> + <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>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>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> </table> |