Quack
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:
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>
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="">
<cfparam name="form.Contract_Number
<cfparam name="form.Contract_Number
<cfparam name="form.Contract_Number
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
In short, it's safer to add the cfparams, and provided good information for future developers to see what fields are being posted
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
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
thnx
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
Any ideas as to why its not catching? params? log isn't showing anything off base.
<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>
Any ideas as to why its not catching? params? log isn't showing anything off base.
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>
Yes you can remove one condition
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
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>
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
try it this way
Check this GIST
https://trycf.com/gist/e9da0265ba43098a1dd1273ffab72906/acf2016?theme=monokai
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}$"
ASKER
that's not working...this is working w/ either of the refinds by itself:
so this works:
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>
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>
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
[/code]
Doesn't make sense, but since it's working.. Just use the two statements together in sequence..
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
got it...changed the OR to AND to get it working:
thanks for your help!
<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>
thanks for your help!
ASKER
thanks for all the help!
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.
Open in new window
Open in new window