Common Javascript Expressions

Explore using JavaScript expressions to improve your forms.


In this video, we explore a few JavaScript Expressions to use in our SharePoint list form. Javascript is used to set today's date in a Date field and validate an 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/

Was this article helpful?

Can’t find what you’re looking for?

Our world-class Customer Success team is here for you.

Contact Support