---
application: "Intervention Validation"
version: "Version 4"
status: "stable"
---

# Hexadecimal Color Rule

## Validate a Hexadecimal Color Code

Explore how to validate hexadecimal color codes also known as web colors with the additional validation rules of Intervention Validation for your Laravel application.

> public Intervention\Validation\Rules\HexColor::__construct(array $lengths = [3, 4, 6, 8])

The field under validation must be a valid [hexadecimal color code](https://en.wikipedia.org/wiki/Web_colors). 

### Parameters

#### lengths (optional)

Pass optionally allowed lengths as an array to check only for shorthand (3 or 4 characters) or full hexadecimal (6 or 8 characters) form or combinations.

### Example

```php
use Illuminate\Support\Facades\Validator;
use Intervention\Validation\Rules\HexColor;

$validator = Validator::make($request->all(), [
    'attribute-key' => new HexColor([3, 6]),
]);
```

---

## Become a Sponsor

### Intervention Validation needs your help to keep the project going

Intervention Validation is non-commercial, open source licensed and completely free to use. The considerable
effort required to maintain and develop the software is only possible with the financial support
of sponsors. There are two ways in which you can support this project.

- Support via [GitHub Sponsors](https://github.com/sponsors/Intervention)
- Support via [Ko-Fi](https://ko-fi.com/interventionphp)