Skip to content

Commit

Permalink
Merge pull request #2562 from dafriend/alpha-num-punct-validation-rule
Browse files Browse the repository at this point in the history
Added rule "alpha_numeric_punct"
  • Loading branch information
lonnieezell authored Feb 17, 2020
2 parents 98d564f + 5c6593b commit d89c88e
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 155 deletions.
7 changes: 4 additions & 3 deletions system/Language/en/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

// Rule Messages
'alpha' => 'The {field} field may only contain alphabetical characters.',
'alpha_dash' => 'The {field} field may only contain alpha-numeric characters, underscores, and dashes.',
'alpha_numeric' => 'The {field} field may only contain alpha-numeric characters.',
'alpha_numeric_space' => 'The {field} field may only contain alpha-numeric characters and spaces.',
'alpha_dash' => 'The {field} field may only contain alphanumeric, underscore, and dash characters.',
'alpha_numeric' => 'The {field} field may only contain alphanumeric characters.',
'alpha_numeric_punct' => 'The {field} field may contain only alphanumeric characters, spaces, and ~ ! # $ % & * - _ + = | : . characters.',
'alpha_numeric_space' => 'The {field} field may only contain alphanumeric and space characters.',
'alpha_space' => 'The {field} field may only contain alphabetical characters and spaces.',
'decimal' => 'The {field} field must contain a decimal number.',
'differs' => 'The {field} field must differ from the {param} field.',
Expand Down
63 changes: 19 additions & 44 deletions system/Validation/FormatRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public function alpha(string $str = null): bool
return ctype_alpha($str);
}

//--------------------------------------------------------------------

/**
* Alpha with spaces.
*
Expand All @@ -78,24 +76,36 @@ public function alpha_space(string $value = null): bool
return (bool) preg_match('/^[A-Z ]+$/i', $value);
}

//--------------------------------------------------------------------

/**
* Alpha-numeric with underscores and dashes
* Alphanumeric with underscores and dashes
*
* @param string $str
*
* @return boolean
*/
public function alpha_dash(string $str = null): bool
{
return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
}

//--------------------------------------------------------------------
/**
* Alphanumeric, spaces, and a limited set of punctuation characters.
* Accepted punctuation characters are: ~ tilde, ! exclamation,
* # number, $ dollar, % percent, & ampersand, * asterisk, - dash,
* _ underscore, + plus, = equals, | vertical bar, : colon, . period
* ~ ! # $ % & * - _ + = | : .
*
* @param string $str
*
* @return boolean
*/
public function alpha_numeric_punct($str)
{
return (bool) preg_match('/^[A-Z0-9 ~!#$%\&\*\-_+=|:.]+$/i', $str);
}

/**
* Alpha-numeric
* Alphanumeric
*
* @param string $str
*
Expand All @@ -106,10 +116,8 @@ public function alpha_numeric(string $str = null): bool
return ctype_alnum($str);
}

//--------------------------------------------------------------------

/**
* Alpha-numeric w/ spaces
* Alphanumeric w/ spaces
*
* @param string $str
*
Expand All @@ -120,8 +128,6 @@ public function alpha_numeric_space(string $str = null): bool
return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
}

//--------------------------------------------------------------------

/**
* Any type of string
*
Expand All @@ -137,8 +143,6 @@ public function string($str = null): bool
return is_string($str);
}

//--------------------------------------------------------------------

/**
* Decimal number
*
Expand All @@ -151,8 +155,6 @@ public function decimal(string $str = null): bool
return (bool) preg_match('/^[\-+]?[0-9]+(|\.[0-9]+)$/', $str);
}

//--------------------------------------------------------------------

/**
* String of hexidecimal characters
*
Expand All @@ -165,8 +167,6 @@ public function hex(string $str = null): bool
return ctype_xdigit($str);
}

//--------------------------------------------------------------------

/**
* Integer
*
Expand All @@ -179,8 +179,6 @@ public function integer(string $str = null): bool
return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
}

//--------------------------------------------------------------------

/**
* Is a Natural number (0,1,2,3, etc.)
*
Expand All @@ -192,8 +190,6 @@ public function is_natural(string $str = null): bool
return ctype_digit($str);
}

//--------------------------------------------------------------------

/**
* Is a Natural number, but not a zero (1,2,3, etc.)
*
Expand All @@ -205,8 +201,6 @@ public function is_natural_no_zero(string $str = null): bool
return ($str !== '0' && ctype_digit($str));
}

//--------------------------------------------------------------------

/**
* Numeric
*
Expand All @@ -219,8 +213,6 @@ public function numeric(string $str = null): bool
return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
}

//--------------------------------------------------------------------

/**
* Compares value against a regular expression pattern.
*
Expand All @@ -240,8 +232,6 @@ public function regex_match(string $str = null, string $pattern, array $data): b
return (bool) preg_match($pattern, $str);
}

//--------------------------------------------------------------------

/**
* Validates that the string is a valid timezone as per the
* timezone_identifiers_list function.
Expand All @@ -257,8 +247,6 @@ public function timezone(string $str = null): bool
return in_array($str, timezone_identifiers_list());
}

//--------------------------------------------------------------------

/**
* Valid Base64
*
Expand All @@ -273,8 +261,6 @@ public function valid_base64(string $str = null): bool
return (base64_encode(base64_decode($str)) === $str);
}

//--------------------------------------------------------------------

/**
* Valid JSON
*
Expand All @@ -288,8 +274,6 @@ public function valid_json(string $str = null): bool
return json_last_error() === JSON_ERROR_NONE;
}

//--------------------------------------------------------------------

/**
* Checks for a correctly formatted email address
*
Expand All @@ -307,8 +291,6 @@ public function valid_email(string $str = null): bool
return (bool) filter_var($str, FILTER_VALIDATE_EMAIL);
}

//--------------------------------------------------------------------

/**
* Validate a comma-separated list of email addresses.
*
Expand Down Expand Up @@ -338,8 +320,6 @@ public function valid_emails(string $str = null): bool
return true;
}

//--------------------------------------------------------------------

/**
* Validate an IP address
*
Expand Down Expand Up @@ -367,8 +347,6 @@ public function valid_ip(string $ip = null, string $which = null, array $data):
return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which);
}

//--------------------------------------------------------------------

/**
* Checks a URL to ensure it's formed correctly.
*
Expand Down Expand Up @@ -397,8 +375,6 @@ public function valid_url(string $str = null): bool
return (filter_var($str, FILTER_VALIDATE_URL) !== false);
}

//--------------------------------------------------------------------

/**
* Checks for a valid date and matches a given date format
*
Expand All @@ -419,5 +395,4 @@ public function valid_date(string $str = null, string $format = null): bool
return (bool) $date && \DateTime::getLastErrors()['warning_count'] === 0 && \DateTime::getLastErrors()['error_count'] === 0;
}

//--------------------------------------------------------------------
}
Loading

0 comments on commit d89c88e

Please sign in to comment.