Regular Expressions Hexadecimal number validation with .net
In mathematics and computer science, hexadecimal (also base-16, hexa, or hex) is a numeral system with a radix, or base, of 16. 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.
Regular Expression Pattern
^[a-fA-F0-9]+$
A description of the regular expression:
Beginning of line or string
Any character in this class: [a-fA-F0-9], one or more repetitions
End of line or string
Successful Matches
ffddee
abcdef
C67fdc
239
be90
How It Works
This regular expression will check for Hexadecimal number contains 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 Any character in this class: [a-fA-F0-9], one or more repetitions 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 number 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 number" ValidationExpression="^[a-fA-F0-9]+$">
</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]+$");
//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]+$")
‘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





[...] Regular Expressions Hexadecimal number validation with .net Regular Expression [...]