Validating a GUID Value with Regular Expressions 

I am sure that most readers of this site have at one point or another noticed my RegularExpression articles.  Well this past week I was looking for a very specific regular expression and thought I would share it with you.  The following regular expresion is used to validate a GUID value for user input.  You can use this to ensure that product keys and other GUID values that you are using are in the proper format. 

^(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$

Posted by Mitchel on Sunday, April 22, 2007
 

Comments

I kinda came up with the same, only a little shorter:

/^\{?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}?$/i

By magikMaker on Tuesday, October 27, 2009 at 12:00 PM

magikMaker, your pattern does not permit uppercase letters A-F. These are legal in GUID values.

By noway on Tuesday, November 03, 2009 at 10:07 AM

Actually, the regex in the comment posted above will work fine, because the author specified that the regex should be case insensitive, meaning that uppercase A-F will match the pattern.

By Nick C on Monday, January 25, 2010 at 9:11 AM

This regex allows unmatched curly braces :/

By mousio on Saturday, April 10, 2010 at 5:02 PM

Name (required)

Email (required)

Website

CAPTCHA image
Enter the code shown above:

Content provided in this blog is provided "AS-IS" and the information should be used at your own discretion.  The thoughts and opinions expressed are the personal thoughts of Mitchel Sellers and do not reflect the opinions of his employer.

Friend of RedGate

www.datasprings.com - DotNetNuke Modules ICG Hosting

Click here for advertising information.

Content in this blog is copyright protected.  Re-publishing on other websites is allowed as long as proper credit and backlink to the article is provided.  Any other re-publishing or distribution of this content is prohibited without written permission from Mitchel Sellers.