PHPackages                             simon-ugorji/octavalidate-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. simon-ugorji/octavalidate-php

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

simon-ugorji/octavalidate-php
=============================

This is a feature-rich form validation Library that helps to validate your forms server-side using sophisticated regular expressions, PHP's inbuilt validation, and validation rules.

v2.1(3y ago)710MITPHPPHP &gt;=5.6.40

Since Jul 28Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Octagon-simon/octaValidate-PHP)[ Packagist](https://packagist.org/packages/simon-ugorji/octavalidate-php)[ Docs](https://octagon-simon.github.io/octavalidate/php/index.html)[ Fund](https://www.paypal.com/donate/?hosted_button_id=ZYK9PQ8UFRTA4)[ RSS](/packages/simon-ugorji-octavalidate-php/feed)WikiDiscussions main Synced today

READMEChangelog (7)DependenciesVersions (8)Used By (0)

octaValidate-PHP V2.1
=====================

[](#octavalidate-php-v21)

This is a feature-rich Library that helps to validate your forms server-side using sophisticated regular expressions, PHP's inbuilt validation, and validation rules.

We have included a demo folder containing some forms with validation rules applied to each of them. Open any of the files in your local server and submit the form.

This Library also helps to validate your frontend forms using JavaScript. [Visit the repository](https://github.com/Octagon-simon/octaValidate)

OTHER RELEASES
--------------

[](#other-releases)

### Octavalidate - JS

[](#octavalidate---js)

Use the JavaScript release of this library to validate your frontend (HTML) forms.

[Visit the repository](https://github.com/Octagon-simon/octaValidate)

### Octavalidate - NodeJS

[](#octavalidate---nodejs)

Use the NodeJS release of this library to validate your forms server-side.

[Visit the repository](https://github.com/Octagon-simon/octaValidate-nodejs)

DOCUMENTATION
-------------

[](#documentation)

Visit the [DOCUMENTATION](https://octagon-simon.github.io/projects/octavalidate/php/) to learn more about this GREAT Library, and play with the forms there!

INSTALL
-------

[](#install)

### COMPOSER

[](#composer)

```
$ composer require simon-ugorji/octavalidate-php

```

### LOCAL

[](#local)

- Download and import the latest release to your project.
- In your project, use the require keyword &amp; include the file **Validate.php**
- Now with the `use` keyword, link the class to your project and create a new instance of the class by passing in the form id as the **first argument** and any configuration as the **second argument**.

```
require 'src/Validate.php';

use Validate\octaValidate;

$myForm = new octaValidate('FORM_ID', 'CONFIG_OPTIONS');
```

How to Use
----------

[](#how-to-use)

- Define validation rules for the form inputs
- Invoke the `validateFields()` method and pass in your validation rules as the first argument, then the fields to validate as the second argument. If no fields are provided, it defaults to the `$_POST` array.

```
//require the library
require 'src/Validate.php';

use Validate\octaValidate;

//create new instance
$myForm = new octaValidate('FORM_ID', 'CONFIG_OPTIONS');

//syntax for defining validation rules
$valRules = array(
  "FORM_INPUT_NAME" => array(
    ["RULE_TITLE", "CUSTOM_ERROR_MESSAGE"]
  )
);

/* If you don't provide a custom error message,
the script will use the default one available
*/

//define rules for each form input name
$valRules = array(
  "uname" => array(
    ["R", "Your username is required"]
 ),
  "email" => array(
    ["R", "Your Email Address is required"],
    ["EMAIL", "Your Email Address is invalid!"]
) );

//begin validation on $_POST fields
if ($myForm->validateFields($valRules, $_POST) === true){
    //process form data here
}else{
  //return errors
  print_r(json_encode($myForm->getErrors()));
}
```

### ABOUT THE VALIDATE FIELDS METHOD

[](#about-the-validate-fields-method)

The `validateFields` method accepts 2 arguments;

- The first argument is an array of your validation rules
- The second argument is the `array` of the fields to validate.

    - This is where the form fields are coming from which can either be the `$_POST` or `$_GET` array. If no value is provided, it defaults to the `$_POST` array.

    ```
    //validate $_POST fields
    $myForm->validateFields($valRules, $_POST);

    //validate $_GET fields
    $myForm->validateFields($valRules, $_GET);
    ```

This method returns a `boolean`.

- `true` means there are no validation errors
- `false` means there are validation errors

> The `validateFields` method does not validate uploaded files! Keep reading to learn how you can validate uploaded files.

VALIDATION RULES
----------------

[](#validation-rules)

Here are the inbuilt validation rules.

- R - A value is required.
- ALPHA\_ONLY - The value must be letters only! (lower-case or upper-case).
- LOWER\_ALPHA - The value must be lower-case letters only.
- UPPER\_ALPHA - The value must be upper-case letters only.
- ALPHA\_SPACES - The value must contain letters or Spaces only!
- ALPHA\_NUMERIC - The value must contain letters and numbers.
- DATE\_MDY - The value must be a valid date with the format mm/dd/yyyy.
- DIGITS - The value must be valid digits or numbers.
- PWD - The value must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters.
- EMAIL - The value must be a valid Email Address.
- URL - The value must be a valid URL
- URL\_QP - The value must be a valid URL and may contain Query parameters.
- USERNAME - The value may contain letters, numbers, a hyphen or an underscore.
- TEXT - The value may contain any of these special characters (. , / () \[\] &amp; ! '' "" : ; ?)

Didn't see the validation rule that you need for your form? Don't worry!

With this library, you have the power to define your custom rule and it will be processed as if it were an inbuilt rule.

CUSTOM VALIDATION RULES
-----------------------

[](#custom-validation-rules)

Syntax for defining a custom rule

```
$myForm->customRule($RULE_TITLE, $REG_EXP, $ERROR_TEXT);
```

Here's a custom rule to validate a password.

```
//require the library
require 'src/Validate.php';

use Validate\octaValidate;

//create new instance
$myForm = new octaValidate('my_form');

//custom password validation
$rule_title = 'PASS';
$reg_exp = '/12345/';
$err_txt = "Please enter 12345";
//build the rule
$myForm->customRule($rule_title, $reg_exp, $err_txt);

//provide the rule title when defining validation rules
$valRules = array(
    "password" => array(
        ["PASS"]
    )
);
```

MORE CUSTOM RULES
-----------------

[](#more-custom-rules)

What if you want to define more validation rules?

All you have to do is to create an array that will contain the rule title as the array's property, and its value is another array containing the rule's regular expression, and error text separated by a comma.

Here's the syntax

```
$RULES = array(
    "RULE_TITLE" => ['REG_EXP', 'CUSTOM_ERROR_MESSAGE']
);
$myForm->moreCustomRules($RULES);
```

Here's a custom username &amp; password validation rules

```
//require the library
require 'src/Validate.php';

use Validate\octaValidate;

$myForm = new octaValidate('my_form');

//custom username & password validation
$rules = array(
    "PASS" => ['/12345/', "Please enter 12345"],
    "UNAME" => ['/simon/', "Please enter simon"]
);

//build the rule
$myForm->moreCustomRules($rules);

//provide the rule title when defining validation
$valRules = array(
    "username" => array(
        ["UNAME"]
    ),
    "password" => array(
        ["PASS"]
    )
);
```

> Note that: All Rule Titles are **case-sensitive!**

ATTRIBUTES VALIDATION
---------------------

[](#attributes-validation)

Currently we have 4 types of attribute validation:

- length validation
- EqualTo validation
- Size validation
- File Validation

All attribute validation follows the syntax below

```
//syntax
$valRules = array(
  "FORM_INPUT_NAME" => array(
    ["ATTRIBUTE_TITLE", "VALUE", "CUSTOM_ERROR_MESSAGE"]
  )
);
```

### LENGTH VALIDATION RULES

[](#length-validation-rules)

You can check the number of characters provided by the user using this validation.

- maxlength (5) - This means that value must be 5 characters or less.
- minlength (5) - This means that value must be up to 5 characters or more.
- length (5) - This means that value must be equal to 5 characters.

For Example;

```
//sample validation
$valRules = array(
  "username" => array(
    ["R", "Your username is required"],
    ["MINLENGTH", "5", "Your username must contain 5 characters or more"]
 ),
  "age" => array(
    ["R", "Your age is required"],
    ["LENGTH", "2", "Your Age must contain 2 digits!"]
  )
);
```

### EQUALTO VALIDATION RULES

[](#equalto-validation-rules)

You can check if two inputs contain the same values using the rule **EQUALTO**. The value of this validation rule must be the other **input name** you wish to check against.

```
//sample validation
$valRules = array(
  "password" => array(
    ["R", "Your password is required"],
    ["EQUALTO", "confirm_password", "Both passwords do not match"]
  )
);
```

### FILE VALIDATION RULES

[](#file-validation-rules)

In File validation, we have rules such as;

- ACCEPT - Use this rule to list out the file extensions allowed for upload. Eg. .png, .jpeg.
- ACCEPT-MIME - Use this rule to list out the file MIME types allowed for upload. It supports a wildcard. Eg audio/\*, image/\*, image/jpeg, image/png.

> It is recommended to use `ACCEPT-MIME` rule to validate file types

- SIZE - This rule makes sure that the size of the file or files provided must be equal to the specified value.
- MINSIZE - This rule makes sure that the size of the file or files provided must be up to the specified value or more.
- MAXSIZE - This rule makes sure that the size of the file or files provided must be the specified value or less.
- others includes; FILES, MINFILES, and MAXFILES

> Note that **size, minsize &amp; maxsize** works on both single or multiple files upload.

For example;

```
//sample validation
$formRules = array(
    //single file upload
    "file" => array(
        ["R"],
        ["ACCEPT", ".mp3, .ogg"],
        ["MAXSIZE", "5mb"]
    ),
    //multiple files upload
    "files" => array(
        ["R"],
        ["ACCEPT-MIME", "image/*"],
        ["MAXFILES", "5"],
        ["MAXSIZE", "50mb"]
    )
);
```

Please refer to the [documentation](https://octagon-simon.github.io/projects/octavalidate/php/file.html) to learn more about file validation.

API METHODS
-----------

[](#api-methods)

CONFIGURATION
-------------

[](#configuration)

We have 3 configuration options:

- stripTags: `Boolean`

    Just like PHP's inbuilt `stripTags` function, this option loops through the POST Array and removes anything enclosed within a tag. Default value is `false`.
- strictMode: `Boolean`

    This option removes extra white space from the start and at the end of a form input and also prevents the user from providing reserved keywords as values. Default value is `false`.
- strictWords: `Array`

    This option alows you to provide words that users are not supposed to submit. For eg \["null", "error", "false"\]. In order to use this option, you must set `strictMode` to `true`.

To use any of these options, provide it as an array and pass it as the second argument when creating an instance of octaValidate.

```
//require the library
require 'src/Validate.php';

use Validate\octaValidate;

//set configuration
$options = array(
  "stripTags" => true,
  "strictMode" => true,
  "strictWords" => ["null", "undefined"]
);
//create new instance
$myForm = new octaValidate('FORM_ID', $options);
```

REFERENCE METHODS
-----------------

[](#reference-methods)

After creating a new instance of the function, the methods below becomes available for use.

```
//create instance of the function
$myForm = new octaValidate('FORM_ID');
```

- `validateFields($valRules, $fieldList)`

    Invoke this method to begin validation on the form fields
- `validateFiles($valRules)`

    Invoke this method to begin validation on uploaded files
- `getForm()`

    This method returns the form ID attached to the validation instance.
- `customRule($RULE_TITLE, $REG_EXP, $ERROR_TEXT)`

    Invoke this method to define your validation rule.
- `moreCustomRules($RULES)`

    Invoke this method to define more validation rules.
- `getVersion()`

    Invoke this method to retrieve the library's version number.

> There are more methods in the documentation, Please refer to the [documentation](https://octagon-simon.github.io/projects/octavalidate/php/api.html) to learn more.

DEMO
----

[](#demo)

- Open any file within the demo folder and submit the form or visit the [documentation](https://octagon-simon.github.io/projects/octavalidate/php/) and submit the forms there.

Author
------

[](#author)

[Simon Ugorji](https://twitter.com/ugorji_simon)

Support Me
----------

[](#support-me)

[Donate with PayPal](https://www.paypal.com/donate/?hosted_button_id=ZYK9PQ8UFRTA4)

Contributors
------------

[](#contributors)

[Simon Ugorji](https://twitter.com/ugorji_simon)

###  Health Score

23

—

LowBetter than 26% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~11 days

Total

7

Last Release

1367d ago

Major Versions

v1.6 → v2.02022-10-02

### Community

Maintainers

![](https://www.gravatar.com/avatar/6453c8b6fb85984d22347b763860c41f3a442f2257a61a9e99d4fbfa92614fcc?d=identicon)[Simon-ugorji](/maintainers/Simon-ugorji)

---

Top Contributors

[![Octagon-simon](https://avatars.githubusercontent.com/u/68190998?v=4)](https://github.com/Octagon-simon "Octagon-simon (12 commits)")

---

Tags

form-validationregular-expressionvalidationvalidation-libraryhtmlFormsregexpvalidation libraryform validatorPHP form validationfile-validatorlength-validator

### Embed Badge

![Health badge](/badges/simon-ugorji-octavalidate-php/health.svg)

```
[![Health](https://phpackages.com/badges/simon-ugorji-octavalidate-php/health.svg)](https://phpackages.com/packages/simon-ugorji-octavalidate-php)
```

###  Alternatives

[ezyang/htmlpurifier

Standards compliant HTML filter written in PHP

3.3k352.1M603](/packages/ezyang-htmlpurifier)[nette/forms

📝 Nette Forms: generating, validating and processing secure forms in PHP. Handy API, fully customizable, server &amp; client side validation and mature design.

54413.6M513](/packages/nette-forms)[symfony/html-sanitizer

Provides an object-oriented API to sanitize untrusted HTML input for safe insertion into a document's DOM.

27941.7M141](/packages/symfony-html-sanitizer)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5396.5M34](/packages/stevebauman-purify)[karser/karser-recaptcha3-bundle

Google ReCAPTCHA v3 for Symfony

1872.6M14](/packages/karser-karser-recaptcha3-bundle)[xemlock/htmlpurifier-html5

HTML5 support for HTML Purifier

1053.2M18](/packages/xemlock-htmlpurifier-html5)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
