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#?

1
  • Have you tried regular expressions? Commented Apr 16, 2021 at 1:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.