Help with regular expressions...

Posted on 2003-02-25
Medium Priority
Last Modified: 2013-12-24
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
Question by:fozzynet
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 17

Expert Comment

ID: 8015932
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 !


Author Comment

ID: 8016019
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.

Expert Comment

ID: 8017680
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:


Accepted Solution

weeezl earned 200 total points
ID: 8018302

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question