Client script to restrict spaces - leading/trailing/intext

Brendan Hallida
Kilo Guru

Hi all,

I have a client script running on a variable that has the following requirements:

  1. trim leading and trailing whitespace
  2. change spaces in the text to _ (underscore)

 

var regex = new RegExp("^[a-zA-Z0-9_]*$");

var mailboxName = g_form.getValue('u_new_shared_mailbox_name');

if(!regex.test(newValue)){
 console.log('if statement working for ' + mailboxName);
//setting values
g_form.setValue('u_new_shared_mailbox_name', mailboxName.toString().replace(/\s/g, '_').trim());

The above works, except if I test with the leading / trailing whitespace only.  If I test with whitespace leading / trailing and a space between it all works perfectly.  Makes me think its an issue with the regex?

Does anyone have any ideas? Thanks in advance!

1 ACCEPTED SOLUTION

Hi Brendan,

I've changed the logic slightly, I'm now just checking if the email contains whitespace. Try the following in your client script:

var mailboxName = g_form.getValue('u_new_shared_mailbox_name'); //getValue returns string

	if(/\s/.test(mailboxName)){ //regex test for whitespace in mailbox name
		console.log('if statement working for ' + mailboxName);
		//setting values
		g_form.setValue('u_new_shared_mailbox_name', mailboxName.trim().replace(/\s/g, '_'));
	}

It works fine in my developer instance so let me know how you get along.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

View solution in original post

6 REPLIES 6

Brent Sutton
Mega Sage

Hi Brendan,

Why don't you just trim the mailboxName before you perform the whitespace to underscore replacement?

var regex = new RegExp("^[a-zA-Z0-9_]*$");

var mailboxName = g_form.getValue('u_new_shared_mailbox_name'); //getValue returns string

if(!regex.test(newValue)){
 console.log('if statement working for ' + mailboxName);
//setting values
mailboxName = mailboxName.trim(); //trim any leading or trailing whitespace
g_form.setValue('u_new_shared_mailbox_name', mailboxName.replace(/\s/g, '_'));

Let me know if this worked for you

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

Hi Brent,

Thanks for your reply! That has the same result actually.  When I only have leading / trailing spaces the if statement does not work.  Nothing is writing to the log.

Hi Brendan,

I've changed the logic slightly, I'm now just checking if the email contains whitespace. Try the following in your client script:

var mailboxName = g_form.getValue('u_new_shared_mailbox_name'); //getValue returns string

	if(/\s/.test(mailboxName)){ //regex test for whitespace in mailbox name
		console.log('if statement working for ' + mailboxName);
		//setting values
		g_form.setValue('u_new_shared_mailbox_name', mailboxName.trim().replace(/\s/g, '_'));
	}

It works fine in my developer instance so let me know how you get along.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

Hi Brent,

Sorry for the delay in response, I had an extra long weekend with Australia Day.

Your code did work so I will mark it as correct, however, I found if I submit the catalog item with leading / trailing whitespace it is trimmed automatically!

Brendan