Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

386
Visualizações
Validation hex and rgba colors using regex in php

text field

I am using Laravel and I am trying to validate a color field. This text box should only allow HEX, rgb, rgba, hsl and hsla colors using regex pattern.

In my controller I have this pattern but it is not validating my field value. Any string will pass the validation.

$this->validate($request, [
    'color' => [
        'required',
        'regex:/(#(?:[0-9a-f]{2}){2,4}|#[0-9a-f]{3}|(?:rgba?|hsla?)\((?:\d+%?(?:deg|rad|grad|turn)?(?:,|\s)+){2,3}[\s\/]*[\d\.]+%?\))/i', // <--- not working
    ],
]);

Thanks.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You should add anchors (^ and $):

'regex:/^(#(?:[0-9a-f]{2}){2,4}|#[0-9a-f]{3}|(?:rgba?|hsla?)\((?:\d+%?(?:deg|rad|grad|turn)?(?:,|\s)+){2,3}[\s\/]*[\d\.]+%?\))$/i',
        ^                                                                                                                     ^

To make the regex apply to the whole input instead of just match it.

But please note that your pattern allows input like rgba(1024, 1023, 0) which is an invalid color.


If you want a more bulletproof regex, use this (demo):

^(\#[\da-f]{3}|\#[\da-f]{6}|rgba\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)(,\s*(0\.\d+|1))\)|hsla\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)(,\s*(0\.\d+|1))\)|rgb\(((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*,\s*){2}((\d{1,2}|1\d\d|2([0-4]\d|5[0-5]))\s*)|hsl\(\s*((\d{1,2}|[1-2]\d{2}|3([0-5]\d|60)))\s*,\s*((\d{1,2}|100)\s*%)\s*,\s*((\d{1,2}|100)\s*%)\))$
about 4 years ago · Santiago Trujillo Relatório

0

Hope this will help you out. In the below regex, i am also taking care of spaces as well,

1. #[a-zA-Z0-9]{6} for matching string like #090f00

2. rgb\((?:\s*\d+\s*,){2}\s*[\d]+\) this will match string like rgb(10, 10, 20)

3. rgba\((\s*\d+\s*,){3}[\d\.]+\) this will match string like rgba(100,100,100,0.9)

4. hsl\(\s*\d+\s*(\s*\,\s*\d+\%){2}\) this will match string like hsl(10,30%,40%)

5. hsla\(\s*\d+(\s*,\s*\d+\s*\%){2}\s*\,\s*[\d\.]+\) this will match string like hsla(120, 60%, 70%, 0.3)

Regex:

#[a-zA-Z0-9]{6}|rgb\((?:\s*\d+\s*,){2}\s*[\d]+\)|rgba\((\s*\d+\s*,){3}[\d\.]+\)|hsl\(\s*\d+\s*(\s*\,\s*\d+\%){2}\)|hsla\(\s*\d+(\s*,\s*\d+\s*\%){2}\s*\,\s*[\d\.]+\)

Regex demo

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda