- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2019 06:53 PM
Hi all,
I have a client script running on a variable that has the following requirements:
- trim leading and trailing whitespace
- 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!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2019 08:17 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2019 07:08 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2019 07:32 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2019 08:17 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019 03:08 PM
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