Lightning Forms – Expressions – Common JavaScript Expressions

In this video, we explore a few JavaScript Expressions to use in our SharePoint list form. Javascript is used to set todays date in a Date field, and also to validate an email as a valid email address using RegEx.

Initially setting date time fields

= new Date()Set the current date time (can also be done just by using SharePoint default column value available in column’s configuration)
= new Date(2014, 5, 3)Set the date to 3rd June 2014. Note: The month (2nd) parameter is 0-based, so January is 0 and December is 11.
= new Date().addDays(10)Set the date to current date (and time) plus 10 days. Use addDays(-n) to subtract n days.
= new Date().addHours(4)Set the date to 2 hours in future.
= new Date().addYears(-1)Set the date a year ago from today.

Calculating date time fields

=[[StartDate]]Get the date time field of another date time field. Note: Use Assignment expression when setting fields using calculated expressions.
=[[StartDate]].addDays(10)Get the date time field of the StartDate column plus 10 days.
{
 if ([[ContractType]] == “Monthly”) 
  return [[StartDate]].addMonths(1);
 else if ([[ContractType]] == “Quarterly”) 
  return [[StartDate]].addMonths(3);
 else if ([[ContractType]] == “Semi-Annually”) 
  return [[StartDate]].addMonths(6);
 else if ([[ContractType]] == “Annually”) 
  return [[StartDate]].addYears(1);
 else return null;
}
Get a date depending on value of field ContractType and StartDate. When ContractType is “Monthly” the date is calculated as StartDate plus 1 month, when ContractType is “Quarterly” the date is calculated as StartDate plus 3 months, etc. If ContractType does not match any of the checked values no date is returned. As an example this function expression could be used as a calculated expression to set the RenewalDate field of a contract item.

Using JavaScript to define list item which is selected on list view

{var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
return items.length == 1;}
Check whether only one item is selected in the list (Can be used in Condition property.)
{var ctx = SP.ClientContext.get_current();
var items = SP.ListOperation.Selection.getSelectedItems(ctx);
if (items.length == 1)  { return items[0].id; }
return null;}

Useful Regular Expressions with the Validation Expressions

Note: all of the below expressions should be entered on the Function tab of the Expression Builder.

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;

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

2 Comments

George Arnold
Reply
8th August 2022 at 3:31 am

Very cool. Perfect formatting solutions. Just need to correctly drop the JS for phone and email formats into the JS feature of Expression Builder. Will watch webinar to try to get that right. Thank you

George Arnold
Reply
8th August 2022 at 3:55 am

My monitor is set at 1920 x 1080, 59.94 Hz, but the webinars are not easy to read. It might be that you want to record the webinars at a higher definition.

Add Comment