Take Only color codes as input

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.