Link to home
Start Free TrialLog in
Avatar of Quack
QuackFlag for United States of America

asked on

Use of cfif, cfelse and cfif...form field validation with 3 sets of criteria...? Syntax isn't working.

I have the code below trying to use cfif and cfelseif to validate on form field, which is for a contract number. I need to use one form field but validate w/ 3 different sets of criteria using 3 different sets of regex validations. Its currently not working and I'm sure the syntax I'm using is off. I'm new to CF much less using cfelseif but I believe I'm on the right track...can anyone provide some suggestions on how to rework this please?

thanks in advance...see code below:
<cftry>
  
   <cfif>
    <cfparam name="form.Contract_Number" pattern="^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$" type="regex" default="#form.Contract_Number#">
    <cfcatch>
		<cfset #session.invoiceDataContr.errorString# = #session.invoiceDataContr.errorString#&"The USCG Contract Number is required and can contain no more than 22 alphanumeric characters,">
    </cfcatch>
      
       <cfelseif>
       
       <cfparam name="form.Contract_Number_Z" pattern="^70Z0\d{4}[CDF]\d{8}$" type="regex" default="#form.Contract_Number_Z#">
       <cfcatch>
		<cfset #session.invoiceDataContr.errorString# = #session.invoiceDataContr.errorString#&"The TSA Contract Number is required and can contain no more than 22 alphanumeric characters,">
       </cfcatch>
        
         <cfelseif> 
                     
        <cfparam name="form.Contract_Number_T" pattern="^70T0\d{4}[CDF]\d{8}$" type="regex" default="#form.Contract_Number_T#">
        <cfcatch>
		<cfset #session.invoiceDataContr.errorString# = #session.invoiceDataContr.errorString#&"The Contract Number is required and can contain no more than 22 alphanumeric characters,">  
        </cfcatch>
         <cfelse>
         
		</cfif>
      
    </cfcatch>

</cftry>

Open in new window

Avatar of gdemaria
gdemaria
Flag of United States of America image

A few simple CFIF statements to see if the variable matches the pattern should do it.

I think you're over using session variables, and IMHO you should not be setting them constantly, if you must use one, then just assign it at the end.

    <cfset errorMsg = "">
    
	<cfif NOT refind("^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$", form.Contract_Number)>
	   <cfset errorMsg = listAppend(errorMsg, "The USCG Contract Number is required and can contain no more than 22 alphanumeric characters",";")>
	</cfif>
	<cfif NOT refind("^70Z0\d{4}[CDF]\d{8}$", form.Contract_Number_Z)>
	   <cfset errorMsg = listAppend(errorMsg, "The TSA Contract Number is required and can contain no more than 22 alphanumeric characters",";")>
	</cfif>
	<cfif NOT refind("^70T0\d{4}[CDF]\d{8}$", form.Contract_Number_T)>
	   <cfset errorMsg = listAppend(errorMsg, "The Contract Number is required and can contain no more than 22 alphanumeric characters",";")>
	</cfif>

Open in new window


    <!--- try not to use session variables, but if you must, just assign it once ---->	
  <cfset session.invoiceDataContr.errorString = errorMsg>

Open in new window

Avatar of Quack

ASKER

would I need to set a cfparam up for each of the contract numbers?

<cfparam name="form.Contract_Number" default="">

<cfparam name="form.Contract_Number_Z" default="">

<cfparam name="form.Contract_Number_T" default="">
You can, if they may not exist.   But if they are always coming from an INPUT type=TEXT form post, they should exist.

In short, it's safer to add the cfparams, and provided good information for future developers to see what fields are being posted
Avatar of Quack

ASKER

should I stay w/in the cftry/cfcatch?
That will catch errors... I suspect your application has an onError function in the application.cfc file to trap errors as well.   You can do it there as well if you wish
and make sure when your session work is complete, just invalidate that specific session or just make it empty because sessions persist and if somehow you are using the same session somewhere else, you might be getting invalid result or invalid message
Avatar of Quack

ASKER

what is the purpose of ";" at the end of the statements? w/in the code the regex doesn't seem to be working as the color scheme isn't matching the other regex values for other fields. wondering if this might be the cause?

thnx
Avatar of Quack

ASKER

The solution isn't working...I only have to validate for 2 sets of criteria now fyi. so its easier ... I currently have this code in place but its catching both validations

    <cfset errorMsg = "">
    
	<cfif NOT refind("^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$", form.Contract_Number)>
	   <cfset errorMsg = listAppend(errorMsg, "The USCG Contract Number is required and can contain no more than 22 alphanumeric characters",";")>
	</cfif>
	<cfif NOT refind("^70Z0\d{4}[CDF]\d{8}$", form.Contract_Number_Z)>
	   <cfset errorMsg = listAppend(errorMsg, "The TSA Contract Number is required and can contain no more than 22 alphanumeric characters",";")>
	</cfif>

Open in new window


Any ideas as to why its not catching? params? log isn't showing anything off base.
Avatar of Quack

ASKER

shouldn't this work for only 2 of the criteria:

    <cfset errorMsg = "">
    
	<cfif NOT refind("^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$", form.Contract_Number) OR NOT refind("^70Z0\d{4}[CDF]\d{8}$", form.Contract_Number)>
	   <cfset errorMsg = "The 5285 Contract Number is required and can contain no more than 22 alphanumeric characters.,">
	</cfif>
	
	<cfset session.invoiceDataContr.errorString = errorMsg>

Open in new window

Yes you can remove one condition
Avatar of Quack

ASKER

yeah...not working for some reason...but is the statement syntax correct w/ the OR in place?
Syntax is correct but try to remove one refine at a time and validate it again and check it
Avatar of Quack

ASKER

will try...would this work do you think as well:

	    <cfset errorMsg = "">
<cfif NOT refind("^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$||"^70Z0\d{4}[CDF]\d{8}$", form.Contract_Number) 
	   <cfset errorMsg = "The 5285 Contract Number is required and can contain no more than 22 alphanumeric characters.,">
	</cfif>

Open in new window

Probably not, because you removed the refind and use the Pipe symbols to create an OR statement, better to write a one refind only and use it first and see if that works or not

try it this way

Check this GIST

https://trycf.com/gist/e9da0265ba43098a1dd1273ffab72906/acf2016?theme=monokai
Avatar of Quack

ASKER

Failing on the first error message
One place for an error is the quote.   Inside your quoted string, you have a quote that is not escaped, so CF thinks it is the end of the string

Change "
"^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$||"^70Z0\d{4}[CDF]\d{8}$"

To ""  (2 quotes together to escape quote)...
"^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$||""^70Z0\d{4}[CDF]\d{8}$"

Open in new window

Avatar of Quack

ASKER

that's not working...this is working w/ either of the refinds by itself:

so this works:
	<cfif NOT refind("^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$", form.Contract_Number)>
	   <cfset errorMsg = "The 5285 Contract Number is required and can contain no more than 22 alphanumeric characters.,">
	</cfif>

Open in new window

and this works:
<cfif NOT refind("^70Z0\d{4}[CDF]\d{8}$", form.Contract_Number)>
          <cfset errorMsg = "The 5285 Contract Number is required and can contain no more than 22 alphanumeric characters.,">
       </cfif>

Open in new window

but not the two together:
[code]	<cfif NOT refind("^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$", form.Contract_Number) OR NOT refind("^70Z0\d{4}[CDF]\d{8}$", form.Contract_Number)>
	   <cfset errorMsg = "The 5285 Contract Number is required and can contain no more than 22 alphanumeric characters.,">
	</cfif

Open in new window

[/code]
Doesn't make sense, but since it's working.. Just use the two statements together in sequence..
Avatar of Quack

ASKER

I'm not sure I'm following you. Are you saying to work it like this:

<cfif NOT refind("^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$", form.Contract_Number) OR NOT refind("^70[Zz][A-Za-z0-9]{1,21}$", form.Contract_Number)>
	   <cfset errorMsg = "The 5285 Contract Number is required and can contain no more than 22 alphanumeric characters.,">
	</cfif>

<cfif NOT refind("^70[Zz][A-Za-z0-9]{1,21}$", form.Contract_Number)>

	   <cfset errorMsg = "The 5285 Contract Number is required and can contain no more than 22 alphanumeric characters.,">
	</cfif>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

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
Avatar of Quack

ASKER

got it...changed the OR to AND to get it working:

	<cfif NOT refind("^[DdFfGgHhNnVv][A-Za-z0-9]{1,21}$", form.Contract_Number) AND NOT refind("^70[Zz][A-Za-z0-9]{1,21}$", form.Contract_Number)>
	   <cfset errorMsg = "The 5285 Contract Number is required and can contain no more than 22 alphanumeric characters.,">
	</cfif> 

Open in new window


thanks for your help!
Avatar of Quack

ASKER

thanks for all the help!