0

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.

1 Answer 1

1

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.

3
  • Hi thanks for the reply and useful information what I meant was to ensure that the card number is valid not the the field this cant be done using regex
    – Sean
    Jul 3, 2022 at 18:57
  • I'm not sure if you mistyped, but I"m not clear what you're comment is trying to clarify. The point of the regex was to condense the amount of characters you're using in your validation rule formula. Likewise, as I mentioned, you can create separate validation rules for each format/card number length. Jul 3, 2022 at 19:20
  • 2
    My understanding is regex is used for patterns I am using the Luhn algorithm to check if a card has a valid number. You second option seems to work I tried creating mutable validation rules for the same field earlier but active was not checked on one and I assumed that you could only have one per field.
    – Sean
    Jul 3, 2022 at 19:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .