The SharePoint out-of-the-box field validation is very limited, and you can't use regex to validate the value entered in the form.
Using skybow Modern Forms as add-in but as well in Solution Studio you can configure a validation on each form field and use the power of regex (or any other way using placeholders or JavaScript).
How to configure
- On the form, click on Configure expression button on the field you want to validate (any type and not just single line of text)
- Open the Validation expression builder in the Expressions configuration panel
- Switch the expression mode to "Function code" (which will accept multi line JS code)
- Add your validation logic that returns true or false. Use regex example from the list below or build your own here: https://regexr.com/
- Don't forget to set a validation text in the "Validation Text" field in the ribbon that should be shown if the validation fails
Examples
Select the field you want to check from the fields list on the right (should be like [[InternalFieldName]])
Email address
if ([[Email]] !== "") {
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/;
return mailformat.test([[Email]]);
} else {
return true;
}
IP address
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;
}
Credit Card (MasterCard)
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;
}
US phone number
if ([[BusinessPhone]] !== "") {
var usphoneformat = /(?:\d{1}\s)?\(?(\d{3})\)?-?\s?(\d{3})-?\s?(\d{4})/g;
return usphoneformat.test([[BusinessPhone]]);
} else {
return true;
}
Replace invalid characters for file name
This file name validation is required when using the Update item action to update a document’s file name. It replaces invalid characters to prevent errors during file renaming.
if ([[Title]] !== "") {
var invalidChars = /[\"*:<>\?\/\\\|\#]/g;
var replaced = [[Title]].replace(invalidChars, "_");
return replaced;
} else {
return [[Title]];
}