Skip to content

isHexColor incorrectly validates hex colors without # #2533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mag123c opened this issue Feb 26, 2025 · 2 comments
Open

isHexColor incorrectly validates hex colors without # #2533

mag123c opened this issue Feb 26, 2025 · 2 comments

Comments

@mag123c
Copy link

mag123c commented Feb 26, 2025

The isHexColor function in validator.js currently allows strings like "6633FF" to pass validation as a valid hex color, even though a proper hex color should include a leading #. This issue arises from the current regex implementation in the library.

https://github.com/validatorjs/validator.js/blob/master/src/lib/isHexColor.js

Is allowing hex colors without # an intended behavior in isHexColor? If not, can I submit a fix?

@WikiRik
Copy link
Member

WikiRik commented Mar 20, 2025

A PR has just been opened for this; #2535
I would say this is intended behaviour in isHexColor. Although HTML and CSS do require a leading #, I think this has been in the project for so long and with the current amount of downloads it could become a breaking change impacting a lot of applications.
What we could do is allow a new option, requireHashtag or something similar, that enforces this behaviour.

@mag123c
Copy link
Author

mag123c commented Mar 20, 2025

The direction you were trying to implement is also consistent with the direction you suggested. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants