php - how to validate color code via regular expression
Get the solution ↓↓↓I going to validate color code via regular expression for example :
#fff : true
#ffffff : true
#fnnkdf : false
255,255,255 : true
255,255 : false
255,255,256 : false
Can you help me?
Answer
Solution:
Give this a try:
/^(#[A-Fa-f0-9]{6}|#[A-Fa-f0-9]{3}|([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))$/
PHP sample:
<?php print_r(preg_match('/^(#[A-Fa-f0-9]{6}|#[A-Fa-f0-9]{3}|([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]))$/','256,123,123')); ?>
What this does is to check if the number is greater than 255.
Simply check with\d{1,3}
or[0-9]{1,3}
will make something like 999,999,999 return true.
Answer
Solution:
This should do the trick:
((\d{1,3},){2}\d{1,3})|(\#[a-fA-F0-9]{3,6})
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.