Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


Regular Expressions Hexadecimal RGB color code validation with .net


Regular Expressions Hexadecimal RGB color code validation with .net

Hexadecimal RGB color also known as Web colors are colors used in designing web pages. Authors of web pages have a variety of options available for specifying colors for elements of web documents. Colors may be specified as an RGB triplet in hexadecimal format (a hex triplet). A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications, to represent colors. It uses sixteen distinct symbols, most often the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or a through f) to represent values ten to fifteen.

We can also use three-digit hexadecimal representation of color in the form #RGB, where RGB is a a three-digit hexadecimal which is expanded to define the six-digit color where each digit is repeated once; thus #RGB defines to the color #RRGGBB.

Regular Expression Pattern

^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$

A description of the regular expression:

Beginning of line or string
A numbered capture group. [[a-fA-F0-9]{6}|[a-fA-F0-9]{3}]
Select from 2 alternatives
Any character in this class: [a-fA-F0-9], exactly 6 repetitions
Any character in this class: [a-fA-F0-9], exactly 3 repetitions
End of line or string

Successful Matches

#ffddee
#abcdef
#C67fdc
#239
#e90
#FF6600

How It Works

This regular expression will check for Hexadecimal RGB color code start with # and the symbols 0–9 to represent values zero to nine, and A, B, C, D, E, F (or a through f) to represent values ten to fifteen. Here we are going to search Beginning of line followed six or three character repetitions in this class: [a-fA-F0-9], and End of line or string.

ASP.NET

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>regular expressions Hexadecimal RGB color code validation with .net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span>Valid Format: #FF00CC</span><br />
<asp:TextBox id="txtInput" runat="server"></asp:TextBox><br />
<asp:RegularExpressionValidator Id="vldRejex" RunAt="server" ControlToValidate="txtInput" ErrorMessage="Invalid Hexadecimal RGB color code" ValidationExpression="^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$">
</asp:RegularExpressionValidator><br />
<asp:Button Id="btnSubmit" RunAt="server" CausesValidation="True" Text="Submit"></asp:Button>
</div>
</form>
</body>
</html>

C#.NET

//use System.Text.RegularExpressions befour using this function
public bool vldRegex(string strInput)
{
//create Regular Expression Match pattern object
Regex myRegex = new Regex("^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$");
//boolean variable to hold the status
bool isValid = false;
if (string.IsNullOrEmpty(strInput))
{
isValid = false;
}
else
{
isValid = myRegex.IsMatch(strInput);
}
//return the results
return isValid;
}

VB.NET

‘Imports System.Text.RegularExpressions befour using this function
Public Function vldRegex(ByVal strInput As String) As Boolean
‘create Regular Expression Match pattern object
Dim myRegex As New Regex("^\#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$")
‘boolean variable to hold the status
Dim isValid As Boolean = False
If strInput = "" Then
isValid = False
Else
isValid = myRegex.IsMatch(strInput)
End If
‘return the results
Return isValid
End Function

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

Thanks for the helping code, it saved my time from doing it by myself, I used the version for C#.net :)

Comment by Abdul Rahman Kayali on March 24, 2010 @ 10:54 am

[...] Regular Expressions Hexadecimal RGB color code validation with .net [...]

Pingback by Text and Data Manipulation with Regular Expressions in .NET Development | Tips n Tracks on April 14, 2010 @ 2:44 pm

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image