Remove spaces from a text field

Hi,
I have a text field for email address. The email address is essential for running a Power shell script so it's important to be captured correctly. However, we've noticed some users entering spaces before and after the email address (most likely copying and pasting) which is causing issues with the script. Is it possible to validate the column and remove the whitespaces that no spaces appear - just the email address. Appreciate I can add a text box as type email but can this be done on a SharePoint field.
Many thanks

Hello @flowy,

Yes, you can remove spaces in the SharePoint text field with the JavaScript.

The below code will remove spaces before saving the record. Please replace FieldName with the Internal Name of the filed in the form and paste it in JavaScript Editor.

fd.spBeforeSave(function(spForm) {

fd.field('FieldName').value = fd.field('FieldName').value.replace(/[\s]/g, "");  

});

That's perfect! Thank you for your help.
Much appreciated!

1 Like

Hi,
I'm trying to implement the same process but for an email address in a public form. Essentially, i've a flow that uses the email address to create a folder but the spacing causes issues with invalid characters. Would this code differ for a public form?
Thanks

Dear @flowy,
Just one small difference, use fd.beforeSave() instead, because sp stands for SharePoint. It should look like this:

fd.beforeSave(function() {
  fd.field('SingleLineText1').value = fd.field('SingleLineText1').value.replace(/[\s]/g, "");  
});

Or you can use field's property Pattern -> Type: Email instead, it will give an error if the email is invalid:
image

1 Like