function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mrunali GaonkarMrunali Gaonkar 

How to compare two strings (alphanumeric) in validation rule?

Does any one have idea to compare two text fields(alphanumeric)?
My requirement is Field1 should always be lesser than Field2.

For eg.
Scenario 1: 
Field1 is ABC000 and Field 2 is XYZ000. In this case, validation will not display an error.

Scenario 2:
Field1 is XYZ000 and Field 2 is ABC000. In this case, validation will  display an error.

Scenario 3: 
Field1 is XYZ006 and Field 2 is XYZ004. In this case, validation will  display an error.

Scenario 3: 
Field1 is XZZ006 and Field 2 is XYZ004. In this case, validation will  display an error.

Thanks!

 
Team NubesEliteTeam NubesElite
Hi Mrunali,
You can compare values like this
Field__1  <>  Field__2

Thank You
www.nubeselite.com

Developement | Training | Consulting

Please mark this as solution if your problem resolved.
Sfdc CoupleSfdc Couple
Hi @Mrunali,

We can do the below workaround for scenario 1,3,4. Please find the below formula for valiadting fields

IF 
( VALUE(SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( field 1, "a", "") , "b", "") , "c", "") , "d", "") , "e", "") , "f", "") , "g", "") , "h", "") , "i", "") , "j", "") , "k", "") , "l", "") , "m", "") , "n", "") , "o", "") , "p", "") , "q", "") , "r", "") , "s", "") , "t", "") , "u", "") , "v", "") , "w", "") , "x", "") , "y", "") , "z", "") , "A", "") , "B", "") , "C", "") , "D", "") , "E", "") , "F", "") , "G", "") , "H", "") , "I", "") , "J", "") , "K", "") , "L", "") , "M", "") , "N", "") , "O", "") , "P", "") , "Q", "") , "R", "") , "S", "") , "T", "") , "U", "") , "V", "") , "V", "") , "X", "") , "Y", "") , "Z", "")) >( VALUE(SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( SUBSTITUTE( field 2, "a", "") , "b", "") , "c", "") , "d", "") , "e", "") , "f", "") , "g", "") , "h", "") , "i", "") , "j", "") , "k", "") , "l", "") , "m", "") , "n", "") , "o", "") , "p", "") , "q", "") , "r", "") , "s", "") , "t", "") , "u", "") , "v", "") , "w", "") , "x", "") , "y", "") , "z", "") , "A", "") , "B", "") , "C", "") , "D", "") , "E", "") , "F", "") , "G", "") , "H", "") , "I", "") , "J", "") , "K", "") , "L", "") , "M", "") , "N", "") , "O", "") , "P", "") , "Q", "") , "R", "") , "S", "") , "T", "") , "U", "") , "V", "") , "V", "") , "X", "") , "Y", "") , "Z", ""))),true,false)

Please let me know if you have queries.

Thanks,
SfdcCopule@gmail.com
 
Mrunali GaonkarMrunali Gaonkar
@Sfdc Couple
I tried this formula in validation but its giving me an error saying (The value '' is not a valid number.)
Sfdc CoupleSfdc Couple
@mrunali 
May I know the data type of the field you are using?
Steve Schreiner 4Steve Schreiner 4
@Mrunali 
Not sure if you are still working on this.
Here's my take:

Compare each string's cooresponding character's Hex value, starting with the first. When one is less, it is "smaller", earlier in the alphabet.
Use the following link to get/create the hex values for each character (expand as necessary).
http://www.cloudywithachanceofcode.com/converting-string-to-decimal-character-array-in-apex/

General code flow:
Get string1 first character hex value.
Get string2 first character hex value.
Compare.
If string1 first character value is smaller than string2 first character value, then string1 is "smaller", no error, breakout of loop.
If string2 first character value is smaller than string1 first character value, then string2 is "smaller", error, breakout of loop.
If equal, get next character value for each string, repeat until they are unequal.