[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Help with regular expressions...

Hi, I'm having a bit of trouble with some regular expressions. What I want to do is validate a form field titled 'base'. The data the form field will be collecting is a domain name. I want basically two rules developed with regular expressions. The first is that the domain name can only contin characters A-Za-z0-9 and a hyphen (-). What I have now is <cfelseif REFindNoCase("^-A-Za-z0-9","#form.base#")> but that doesn't work correctly. The second rule I need is that the domain name cannot start nor end with a hyphen and I have no idea where to even start with that one. Please help.

Thank you,
Greg Walker
1 Solution
i wonder y u need regexp for this

did u try using javascript for this ???

try calling this function onsubmitting the form - so valdate the domain name

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    var sCont = document.#FormName#.#sName#;
    var sContVal = document.#FormName#.#sName#.value;    
    var sSpace = sContVal.indexOf(' ')
    var span=new RegExp("[ ]","g");
    var rep=sContVal.replace(span,"9");
    <CFSET UserNameConstraintsList = "~,`,!,@,##,$,%,&,*,_,+,=,|,/,?,>,<,(,),[,],{,},'">
    <CFLOOP INDEX="PureTextList" FROM="1" TO="#ListLen(TextList)#">
         <CFSET CurTextVal = #ListGetAt(TextList,PureTextList)#>
        var spans#PureTextList#=new RegExp("[#CurTextVal#]","g");
        var rep=rep.replace(spans#PureTextList#,"@");
    var chkindex = rep.indexOf('^')
    var chkslash =  sContVal.indexOf('\\')    // checks for slash and Double quotes
    var chkQuotes =  sContVal.indexOf('"')
    if(sContVal.length == 0 || chkindex != -1 || sContVal=="" || chkslash!= -1 || chkQuotes != -1 || sSpace  != -1)
         return false;                    

u'll need to add something to this to chk up the domain name - i'll work on this !

fozzynetAuthor Commented:
I didn't use javascript because I'm fairly new to coldfusion and do not have any knowledge with the language. I actually want the error message not to appear in a pop-up javascript window but have the page reload and display the error above the form. I know that the javascript way would be more efficient but the errors in a pop up window wouldn't match with the rest of my form. Here is my current code:

<cfif NOT IsDefined("form.submit")>
<form action="whois.cfm" method="post" name="whois">
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
          <td bgcolor="#999999"><strong>Choose Your Domain Name</strong></td>
          <td bgcolor="#6684B1"><b>Step 1 of 5</b></td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <td height="5"></td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#999999">
          <td width="25%" height="30" bgcolor="#cccccc">Requested Domain: </td>
          <td width="75%" bgcolor="#cccccc">
          <input name="base" type="text">
          <select name="extension">
            <option value=".com">.com</option>
            <option value=".net">.net</option>
            <option value=".org">.org</option>
               <option value=".us">.us</option>
               <option value=".info">.info</option>
               <option value=".biz">.biz</option>
<input type="submit" name="Submit" value="Submit">
<cfif IsDefined("form.submit")>
<cfset domainStart = "#form.base##form.extension#">
<cfset domain = REReplace("#domainStart#","[[:space:]]","","ALL")>
<cfset WhoisLocation = "/usr/local/bin/whois -s">
<cfset CustomID = RandRange(1,10000000)>
<cfset thisPath = "/home/httpd/vhosts/order.fozzy-networks.com/httpdocs/temp/">
<cfset thisDirectory = GetDirectoryFromPath(thisPath)>
<cfset OutputFile = "#thisPath##CustomID#.txt">
     <cfif len(#form.base#) GT 63>
     <cfoutput>Domain must be 63 or fewer characters.</cfoutput>
     <cfelseif REFindNoCase("^-[[:punct:]]","#form.base#")>
     <cfoutput>Domain contains invalid characters.</cfoutput>
     <cfelseif NOT REFindNoCase('',#form.base#)>
     <cfoutput>Please provide a domain name.</cfoutput>
     <cfelseif len(#form.base#) LT 3>
     <cfoutput>Domain must be be at least 3 characters.</cfoutput>
          <cfexecute name="#WhoisLocation#" arguments="#domain#" outputfile="#OutputFile#" timeout="3"></cfexecute>
          <cfset filecreated=true>
          <cffile action="READ" file="#OutputFile#" variable="message">
          <cfif FindNoCase("No match for domain",message)>
               <cflocation url="http://order.fozzy-networks.com/form.cfm?Domain=#domain#">
          <cfelseif FindNoCase("NOT FOUND",message)>
               <cflocation url="http://order.fozzy-networks.com/form.cfm?Domain=#domain#">
          <cfelseif FindNoCase("Not found",message)>
               <cflocation url="http://order.fozzy-networks.com/form.cfm?Domain=#domain#">    
               <cfoutput>Domain is taken. Please select another.
                    <form action="whois.cfm" method="post" name="whois">
                   <input name="domain" type="text"><br>
                   <input type="submit" name="Submit" value="Submit">

Let me know if I can use that javascript in this way and if so, what is the exact onsubit function? Thanks for your help. I appreciate it.
Here is a good place with some info about CF Regular expressions:


And this one lists out some info for Regular Expressions through JavaScript. But all regular expressions are VERY similar, and this will help you get some more detailed ideas and understanding:


You should look into regular expressions. For example, here is a single expression, that all in one line, will validate an email address based on the following characteristics:

1. email contains an "@" and it is not the first or last character in the email
2. email has an "." and it is not the first or last character in the email
3. the only characters allowed in the email address are alphanumerics, periods, or hyphens
4. The only characters allowed in the domain ending are alpha and it is between 2 and 4 characters long

<cfif REFindNoCase("^[[:alnum:]_\.\-]+@([[:alnum:]_\.\-]+\.)+[[:alpha:]]{2,4}$", EMail, 1) EQ 0>

I'm sure you can do something similar for validating your domain. This example came from a custom tag called CF_mailtest. If you download it, you can take a look at the logic and some other logic that was put in to determine the actual cause of the problem if the above string returned 0.


Oh, and here's an easy way to test if the domain name starts or ends with a hyphen:

<cfif Left(base, 1) EQ "-" or Right(base, 1) EQ "-" >


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now