skip to content »

Regex for validating domain name

regex for validating domain name-49

The ending quotation mark must not be preceded by a backslash character (\).

Additional logic is required to prevent something like passing as a valid domain name or to confirm that the domain is actually hosted somewhere—but I’ll leave that as an exercise for the reader. From Milliseconds(200)) Catch e As Regex Match Timeout Exception Return False End Try If invalid Then Return False ' Return true if str In is in valid e-mail format. Instead of using a regular expression to validate an email address, you can use the System. \\.) ((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/i It checks against all known tlds and ensures that the domain name begins and ends with an alphanumeric character, allowing for dashes and sub-domains.Single character domains are allowed (did you know Pay Pal owns are ways of going even further and making sure that, when you talk to the recipient’s SMTP server, that it responds accordingly, but that’s one step too far for me right now.

;) Can easily be extended to only check for nameservers and such … Note that this does not work in Windows/BSD systems, but the PEAR Net_DNS package can provide this for those platforms. Stepping around the validation issue for a minute, if a customer doesn’t want to give you a valid e-mail/domain is that someone you really want to be doing business with?

The assumption is that the TLD should be atleast two letters.

into two separate regexes, one for IP addresses and the other for domain names.

Note that the regular expression is compiled using the System. This pattern can be repeated one or more times, and must be followed by the top-level domain name.

[0-9a-z]*.) [a-z0-9][-a-z0-9][a-z0-9]))$` is interpreted as shown in the following table. If the character that follows @ is not an opening bracket, match one alphanumeric character with a value of A-Z, a-z, or 0-9, followed by zero or more occurrences of a word character or a hyphen, followed by zero or one alphanumeric character with a value of A-Z, a-z, or 0-9, followed by a period. To determine whether an email address is valid, pass the email address to the System.

A really common example of this is the The Power Shell script I have written requires a username, which is then used to delete alerts in Sharepoint for that user. The check is just for a string of the correct format - since it takes a little while to process the script on a large Sharepoint install I want to check the syntax at least is correct otherwise it takes 10 - 30 seconds to find out you made a typo.