Lightning Forms – Custom Field Validation

On this page, we will explore field validation. Validation expressions provide an easy way to define a validation rule. Even complex rules can be defined using expressions to specify required form input, and so, if the validation expression evaluates to false, the form cannot be saved. We will look at a video exploring validation within Lightning Forms and highlight other scenarios where validation can be applied.

In the training video below, we explore how to use custom validation within Lightning Forms. We use simple Required validation to ensure a field is completed, a simple expression validation with a specific validation message, and a regular expression validation using javascript.


Some useful examples of regular expressions can be found below:

Valid Email Address Regular Expression
if([[Email]]!==""){ var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; return mailformat.test([[Email]]); } else return true;
IP Address Regular Expression
if([[IPAddress]]!==""){ var ipformat = /\b(?:(?:2(?:[0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9])\.){3}(?:(?:2([0-4][0-9]|5[0-5])|[0-1]?[0-9]?[0-9]))\b/ig; return ipformat.test([[IPAddress]]); } else return true;
MasterCard Regular Expression
if([[MasterCard]]!==""){ var mcformat = /^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$/g; return mcformat.test([[MasterCard]]);
} else return true;
Phone Number US Format Regular Expression
if([[WorkPhone]]!==""){ var usphoneformat = /(?:\d{1}\s)?\(?(\d{3})\)?-?\s?(\d{3})-?\s?(\d{4})/g; return usphoneformat.test([[WorkPhone]]);
} else return true;


There are other scenarios where field validation can come in handy:


LIMITING THE CHARACTER LENGTH OF A FIELD

You might want to restrict the number of characters entered into a field. In the example below, the validation condition code ensures that the TestListMultiline column has less than or equal to 255 characters.



SETTING THE VALUE OF A FIELD BASED ON THE CONTENTS OF ANOTHER FIELD

You might also want to set a field’s value based on the contents of another field. In this example, we want to multiply the values of two fields only if another field has a certain value.

There are these columns: Owner Text, Number Column, Number 2 and Number 3.


We want to do a calculation in [[NumberColumn]] such that [[Number2]]*[[Number3]] would only work when [[OwnerText]] is “Demo User2”. We set the validation condition, validation text, and initial value as shown below.


As per the expressions laid out, the [[Number2]]*[[Number3]] calculation only works for the list item that has Demo User2 under the Owner Text column. The form is not saved for the other two list items (4*8 and 2*8), and the validation text is displayed.


Validation expressions can be applied to many other situations to ensure that forms are only saved when the field inputs meet certain conditions.


Useful Resources:

Regular Expression Syntax help: https://www.w3schools.com/jsref/jsref_obj_regexp.asp

Regular Expression Test Tool: https://regexr.com/

Related Posts
Clear Filters

Add Comment