Skip to content

isRgbColor: alpha values with more than 2 decimal digits or trailing zeros on '1' are incorrectly rejected #2792

Description

@JSap0914

Describe the bug

isRgbColor rejects valid CSS rgba() colors where the alpha value has more than 2 decimal digits, or where alpha is 1.00 (a valid number equal to 1).

Examples

const validator = require('validator');

validator.isRgbColor('rgba(0,0,0,1.00)');   // false — expected true
validator.isRgbColor('rgba(0,0,0,0.123)');  // false — expected true
validator.isRgbColor('rgba(255,255,255,1.00)'); // false — expected true
validator.isRgbColor('rgba(0%,0%,0%,0.123)');   // false — expected true

// These DO work (2 or fewer decimal digits):
validator.isRgbColor('rgba(0,0,0,1.0)');    // true  ✓
validator.isRgbColor('rgba(0,0,0,0.12)');   // true  ✓

Root cause

In src/lib/isRgbColor.js, the alpha pattern is:

(0?\.\d\d?|1(\.0)?|0(\.0)?)
  • 0?\.\d\d? matches at most 2 decimal places (.X or .XX)
  • 1(\.0)? only matches 1 or 1.0, not 1.00, 1.000, etc.
  • 0(\.0)? similarly only matches 0 or 0.0

The CSS specification defines <alpha-value> as a CSS <number>, which has no limit on decimal precision:

Expected fix

Extend the alpha pattern to allow any number of decimal digits and trailing zeros on boundary values 0 and 1:

(0?\.\d+|1(\.0+)?|0(\.0+)?)

Validator.js version: 13.15.35

Node.js version: 22+

OS platform: linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions