Copy link to clipboard
Copied
Can anyone help with completing a custom validation to compare a string that is all uppercase, alphanumeric, and will compare several strings that should be considered "not a valid product number?" The three strings would start with letters as follows: IP000000, IF000000, and RL000000 (the fail statements should be specific and start with IP, IF, and RL).
I'm still new to editing custom validations, and can take any help! Thank you
Copy link to clipboard
Copied
RegEx to the rescue!
Assuming all string values will be the same length, every time.
<cfset IPcode = "IP123456" />
<cfset IFcode = "IF147258" />
<cfset RLcode = "RL963852" />
<cfset badCode = "TT99RR12" />
<cfset goodOne = REmatch("^([IP|IF|RL]){1}\d{6}$",IPcode) /><!--- Must match a string that begins with IP, IF, or RL, followed by six digits. --->
<cfif arrayLen(goodOne) eq 1>IT'S A MATCH<cfelse>Not a match</cfif>
<cfset goodOne = REmatch("^([IP|IF|RL]){1}\d{6}$",IFcode) />
<cfif arrayLen(goodOne) eq 1>IT'S A MATCH<cfelse>Not a match</cfif>
<cfset goodOne = REmatch("^([IP|IF|RL]){1}\d{6}$",RLcode) />
<cfif arrayLen(goodOne) eq 1>IT'S A MATCH<cfelse>Not a match</cfif>
<cfset badOne = REmatch("^([IP|IF|RL]){1}\d{6}$",badCode) />
<cfif arrayLen(badOne) eq 1>IT'S A MATCH<cfelse>Not a match</cfif>
REmatch() returns an array that is populated with all of the matches it finds. Since we are matching the whole string by using ^ and $, matches will produce an array of exactly one, anything else will return an empty array (length of 0.)
HTH,
^ _ ^
UPDATE: I'm going to create a new response to your question. I just noticed the part about being specific to each type.
No, I'm not. I'm going to calmly wait until you read this response, in case I'm missing something. And to allow you to ask questions.
Copy link to clipboard
Copied
Using regex, as WolfShade has done, is great. That's one way. You could cut it in many other ways, for example
<cfset testString="ip123456">
<cfswitch expression="#left(uCase(testString),2)#">
<cfcase value="IP">
<!--- Test for IP string--->
<cfset stringLength=len(testString)>
<cfset numericPart=right(testString, stringLength-2)>
<cfset isPartNumeric=isNumeric(numericPart)>
<!--- etc. --->
</cfcase>
<cfcase value="IF">
<!--- Test for IF string--->
</cfcase>
<cfcase value="RL">
<!--- Test for RL string--->
</cfcase>
<cfdefaultcase>
<!--- Test for string that is not IP, IF or RL--->
</cfdefaultcase>
</cfswitch>
Copy link to clipboard
Copied
Oh, and to take letter-case into account
<cfset testString="IP123456">
<!---<cfset testStringUCase="IP123456">--->
<cfset initial2Chars=left(testString,2)>
<cfswitch expression="#initial2Chars#">
<cfcase value="IP">
<!--- Test for IP string--->
<cfset areInitial2CharsinUCase = compare(initial2Chars,uCase(initial2Chars))><!---0 if true --->
<cfset stringLength=len(testString)>
<cfset numericPart=right(testString, stringLength-2)>
<cfset isPartNumeric=isNumeric(numericPart)>
<!---etc --->
</cfcase>
<cfcase value="IF">
<!--- Test for IF string--->
</cfcase>
<cfcase value="RL">
<!--- Test for RL string--->
</cfcase>
<cfdefaultcase>
<!--- Test for string that is not IP, IF or RL--->
</cfdefaultcase>
</cfswitch>