I hit maximum character allowance when validating a card number what are the alternatives

apexvalidationvalidation-rulevisual-workflow

I am validating a field for credit card numbers but I want to be able to check if it is American Express, Visa, or MasterCard as each of these cards have a different amount of numbers.

I have tried using a validation rule which works but this does not allow me to check for cards with a different amount of digits.This is a short version of the 16 digits which are in my working version.

MOD(IF( VALUE(MID(Credit_Card__c, 1, 1))<5, 
VALUE(MID(Credit_Card__c, 1, 1))*2, 

VALUE(MID(TEXT(VALUE(MID(Credit_Card__c, 1, 1))*2),1,1))
+VALUE(MID(TEXT(VALUE(MID(Credit_Card__c, 1, 1))*2),2,1)) 
)
 
+ 
VALUE(MID(Credit_Card__c, 2, 1))
+

IF( VALUE(MID(Credit_Card__c, 3, 1))<5, 
VALUE(MID(Credit_Card__c, 3, 1))*2, 

,10) <> 0 

)

I tried to add an 'IF' statement to check if the 'TEXT' length is 16 or 15 to match the digits in the card but I get the error stating my validation rule is to long (3950 characters I think) because I add another full validation this time with only 15 numbers instead of 16.

IF( LEN(Credit_Card__c) = 16,

I am not sure if this is the correct way of going about validating a card would I be better using a process flow to do this and use APEX code to verify the card? I am confused though how to display an error message to the user if the card is not valid? Any help or direction in what I should do would be appreciated.

Best Answer

When you run out of characters in a validation rule formula, you're typically left with a couple options to consider:

  1. Is this written efficiently? Can it be shortened and use less characters?
  2. Does this have to all be in one validation rule formula?

If the answer is "no" to both of those - then you'd have to look to provide the validation through a different mechanism (apex trigger).


For your case, both answers to those questions appear to be "yes"

  1. Using less characters

Salesforce has a list of Sample Validation Rules with one specifically about credit card validation (16-digit version). You'll notice, with regex, it's a lot less characters.

NOT( REGEX( Credit_Card_Number__c , "(((\\d{4}-){3}\\d{4})|\\d{16})?"))

They have explanations for what it's doing - but, you'll have to go through a similar exercise for the other variations you're looking to validate.

  1. Make multiple validation rules

If the above doesn't pan out (still too many variations, checks, etc) - then instead of making one giant validation rule that takes into account x amount of formats, make one validation rule for each format. You'll start each validation rule with the check for length to dictate which format you'll be focusing on.

This affords you the room for the characters necessary. Likewise, each validation rule can point to the same field and display an error message based on the format expected.

Related Topic