Link to home
Start Free TrialLog in
Avatar of fozzynet
fozzynet

asked on

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
Avatar of anandkp
anandkp
Flag of India image

hi,
 
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#,"@");
    </CFLOOP>
   
    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)
    {
        alert("#sLabel#");
         sCont.focus();
         sCont.select();
         return false;                    
    }
//-->
</SCRIPT>                

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

K'Rgds
Anand
Avatar of fozzynet
fozzynet

ASKER

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">
     <tr>
          <td bgcolor="#999999"><strong>Choose Your Domain Name</strong></td>
     </tr>
     <tr>
          <td bgcolor="#6684B1"><b>Step 1 of 5</b></td>
     </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
     <tr>
          <td height="5"></td>
     </tr>
</table>                
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#999999">
     <tr>
          <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>
          </select></td>
     </tr>
</table>
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
</cfif>
<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>
     <cfelse>
          <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#">    
          <cfelse>
               <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">
                    </form>
               </cfoutput>
          </cfif>
     </cfif>
</cfif>

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:

http://telecom.fit.edu/cfdocs/lang/lr040004.htm

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:

http://www.devguru.com/Technologies/ecmascript/quickref/regexp_special_characters.html
ASKER CERTIFIED SOLUTION
Avatar of weeezl
weeezl

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial