Web Hosting Talk







View Full Version : regex - need to validate domain in ColdFusion


gm22
04-27-2004, 09:50 AM
Hi,
This is a regex question. I have a form that posts a domain name and extention (e.g., domain=test and extention=.com)

I need to be able to check that the domain value
1. starts and ends with a letter or a number
2. only has letters, numbers and "-" in the middle.

I need to use it with ColdFusion.

Any help?
Thanks.

slack
04-27-2004, 12:52 PM
Something like....

^(\w+-?\w*)*\w$

should do it for the domain name. Basically it says that you must have one or more alphanumeric characters, followed by zero or one hypen, followed by one or more alphanumeric characters, repeating indefinately (e.g. so-you-could-have-a-name-like-this.com).

For the top level, you could either match against the known list (.com, .org, .biz, etc), or...

^\.\w{2,4}$

which would match a dot followed by any 2-4 character alphanumeric string. Get rid of the slash dot if they don't put the dot in the form field.

slack
04-27-2004, 01:08 PM
Note: if you test these expressions via some regex checker, make sure it understands ^ and $...I've found that most of the online ones do not correctly interpret these, and they are critical in making the above expressions work.

lwknet
04-28-2004, 12:58 PM
Originally posted by slack
Something like....

^(\w+-?\w*)*\w$

should do it for the domain name. Basically it says that you must have one or more alphanumeric characters, followed by zero or one hypen, followed by one or more alphanumeric characters, repeating indefinately (e.g. so-you-could-have-a-name-like-this.com).

For the top level, you could either match against the known list (.com, .org, .biz, etc), or...

^\.\w{2,4}$

which would match a dot followed by any 2-4 character alphanumeric string. Get rid of the slash dot if they don't put the dot in the form field.

can point out some regex checkers to check with?

dan_erat
04-28-2004, 01:28 PM
"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" is a Perl-compatible regex that matches what you want. I dunno if ColdFusion supports PCREs or not.

nnormal
04-29-2004, 10:24 AM
this is how we grep in CF


<!--- Ending FYDate Search --->

<cfif isDefined ("attributes.FYEnd") and attributes.fyend neq "" and REFind("^[[:digit:]]{4}$",attributes.fyend)>
AND FYEnd CONTAINS ('#attributes.FYEnd#')
</cfif>

<!--- Abstract Search --->

<cfif isDefined ("attributes.pabstract") and attributes.pabstract neq "" and REFind("[[:alpha:]]+",attributes.pabstract)>
AND #MakeFQM("pabstract",attributes.pabstract)#
</cfif>

dan_erat
04-29-2004, 11:08 AM
Ick. Anyway, that regular expression I posted above should do the trick.