skip to Main Content

I will take string from the variable and it will only accept the colour codes of any type like rgba, hashcode and rgb etc. so how can i create that variable that only accept this type of inputs?

i want this solution in javascript.

2

Answers


  1. You can use CSS support API

    You can use this API to validate the input

    CSS.supports(propertyName, propertyValue)
    
    console.log(CSS.supports('color','blue') )
    console.log(CSS.supports('color','#FFFFFF') )
    console.log(CSS.supports('color','xyz') )
    console.log(CSS.supports('color','rgb(0,0,0)') )
    Login or Signup to reply.
  2. Yes , i can do that , here is solution for that

    const str = "This is a #FF0000 red color text and #0000FF blue color text.";
    const regex = /#(?:[0-9a-fA-F]{3}){1,2}b|b(?:rgb|hsl)a?((?:(?:d{1,2}|[1-2]d{2}|3[0-5]d)(?:s*,s*)){2}(?:d{1,2}|[1-2]d{2}|3[0-5]d)(?:s*/s*(?:0.d+|d+(?:.d+)?))?)/g;
    const hexColors = str.match(regex);
    console.log(hexColors); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search