We always try to make our Expression Builder as simple as possible. This makes it easy to use without extensive programming knowledge. However, it can still happen in various places that individual JavaScript expressions are required.
We have summarized the most frequently used ones for you here. We hope that this will provide you with good assistance in configuring advanced expressions.
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(2024, 5, 3) | Set the date to 3rd June 2024. 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 4 hours in future. |
= new Date().addYears(-1) | Set the date a year ago from today. |
How co calculate 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. |
How to use JavaScript to define a list item which is selected on the 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 validation expressions
Note: All of the below expressions need to 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 advanced resources
Regular Expression Syntax help: https://www.w3schools.com/jsref/jsref_obj_regexp.asp
Regular Expression Test Tool: https://regexr.com/