0

I want to validate if a string is valid ARGB.

E.g. #ffffffff is ARGB 255,255,255,255

How can I validate this using TypeScript and C#?

Chatra
  • 2,989
  • 7
  • 40
  • 73

1 Answers1

3

For either language, you can use regular expressions to ensure a string matches your expectations. Here are some examples in Typescript and C#.

Typescript

const argbRegex = /^#[0-9a-fA-F]{8}$/;
argbRegex.test("#ffffffff"); // true
argbRegex.test("#gggggggg"); // false

C#

Regex argbRegex = new Regex("^#[0-9a-fA-F]{8}$");
argbRegex.IsMatch("#ffffffff"); // True
argbRegex.IsMatch("#gggggggg"); // False

Since you didn't mention it in your question, there are some things that you might want to consider that I did not cover:

  • Should a 3 character RGB string be a valid ARGB string?
  • Should a 6 character RGB string be a valid ARGB string?
  • Do you strictly require the # symbol?
  • Do you need to extract the decimal values for each ARGB component?

However, if all you need to do is validate the string is strictly in the 8 characters ARGB format, the above examples will work ok.