Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine


Validate With Regular Expressions (Continued)

Validate a Domain Address
The example I've just shown you demonstrates the most common constructs you use when you write regular expressions to validate user input. Here's another simple example that shows how you can use other input characters to validate input (see Listing 2). Suppose you want to parse a domain address. The addresses fawcette.com, microsoft.com, and srtsolutions.com are all valid. However, any address that uses a different protocol (such as ftp://) or contains invalid characters is invalid. A valid domain name must have fewer than 64 characters, which can include only a–z, 0–9, and -. You must have no more than 63 characters, no more than one period (.), and a suffix in order to validate a domain name. I'll limit the list of valid suffixes to .com, .net, .org, .edu, .gov, and .mil, to keep this example reasonably simple.

ADVERTISEMENT

Once again, you build the regular expression by using ^ and $ for the start and end of the entire input string. You need to find between 1 and 63 characters in the set of a–z, 0–9, and -. You place the set of valid character ranges inside the brackets. This set includes the range a–z, the range 0–9, and the single hyphen character. The {1,63} construct matches from 1 to 63 repeats of the preceding range. This expression builds on a construct you saw previously:

[a-z,0-9,-]{1,63}

Next, you must find a single period. This might seem simple, but a period is a special character in the regular expression language, so you need to escape the character by preceding it with a backslash (\). Finally, you must find one of the approved extensions. You find one of a set of phrases by placing all the phrases between parentheses, separated by the pipe character (|): (com|net|org|edu|gov|mil). The complete expression is:

^[a-z,0-9,-]{1,63}\\.
(com|org|net|gov|mil)$

Note that you need only two lines of code for each expression in the examples I've shown you. Putting together regular expressions can take work, but it pays off handsomely. I've yet to see an input format that you can't validate with regular expressions. Look at the samples provided in the ASP.NET regular expression validator to learn more about using and forming your own regular expressions. Try to understand how each one works. Then, build expressions for your own validations. Test all the small subexpressions individually and include comments in your code, because debugging a long expression can be tricky.

About the Author
Bill Wagner is SRT Solutions' Windows technology expert. He is a contributing editor for Visual Studio Magazine and the author of C# Core Language Little Black Book, an advanced reference for C# developers. Bill has had a lead design role in many projects in his 16 years of software development. He has designed software for engineering and business applications, for desktop and Web environments. He has an extensive background in 2-D and 3-D graphics and multimedia software, including developing the video playback engine used for "The Lion King Animated Storybook." Reach him at .

Back to top

Printer-Friendly Version













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home