Lightning Forms – Custom Field Validation

In this training video, 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 for regular expressions can be found below:

Valid Email Address Regular Expression
 var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; 
 return mailformat.test([[Email]]);
 } else 
 return true;
IP Address Regular Expression
 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
 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
 var usphoneformat = /(?:\d{1}\s)?\(?(\d{3})\)?-?\s?(\d{3})-?\s?(\d{4})/g;
 return usphoneformat.test([[WorkPhone]]);
} else 
 return true;

Useful Resources:

Regular Expression Syntax help:

Regular Expression Test Tool:

Leave a comment