0

I'm trying to create a simple validation rule that checks the following

  • If a value is entered, make sure it's 15 characters long
  • If a value is deleted or made blank, let them save the record

Not sure where its going wrong. Here's the formula

OR (
ISCHANGED( field ) && LEN( field ) < 15, 
ISCHANGED( field ) && ISBLANK( field ))

1 Answer 1

1

When a validation rule returns true, it prevents the record from being saved.

This runs counter to typical developer intuition (it's very easy to try to treat a validation rule like an if/else, mentally).

The better way to frame validation rules is what needs to happen for us to complain? In your case, that would be

  • If the field has changed
  • And the field length is not 15
  • And the field is not blank

So using AND() instead of OR() here, and inverting the blank check is what you want to do. When the field is 15 characters, it doesn't satisfy all 3 conditions. When the field is blank, it doesn't satisfy all 3 conditions.

AND(
    ISCHANGED(field),
    NOT(LEN(field) = 15),
    NOT(ISBLANK(field))
)

Side note: It's usually a good idea to avoid mixing notation in formulas/validations. Choose to use AND() and OR(), or && and || and stick with it. Otherwise, you're setting yourself up for semantic errors (i.e. things have a higher chance of being evaluated in a way that you didn't intend).

You must log in to answer this question.

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