• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Comparison validation script

New Here ,
May 30, 2018 May 30, 2018

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

Views

181

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2018 May 31, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2018 Jun 03, 2018

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2018 Jun 03, 2018

Copy link to clipboard

Copied

LATEST

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation