I am using C # and ASP.NET 4 WebControls.
I have a TextBox on my page. The user can enter the HTML color in HEXADECIMAL format (ff0000) or in HTML format ("Red").
My initial thought was that it would be too difficult to write RegEx capable of checking this user input, so I came up with the idea of writing a simple method to check if the input color can be converted to a valid one, which the System.Drawing context will use.
Below is my code. It returns a Bool DataType indicating whether the operation was successful. Now it works fine, but I would like to know:
- If my method was well written?
- Do you know a better approach?
Thank you for your attention.
using SD = System.Drawing;
protected static bool CheckValidFormatHtmlColor(string inputColor)
{
try
{
SD.Color myColor = SD.ColorTranslator.FromHtml(inputColor);
return true;
}
catch (Exception ex)
{
return false;
}
}