Trim only leading and trailing spaces from a field | Community
Skip to main content
Geckoz100
Level 5
March 22, 2016
Solved

Trim only leading and trailing spaces from a field

  • March 22, 2016
  • 7 replies
  • 10077 views

Is there a way to trim only the leading and trailing spaces from a field?  I have a Name field, which I later split so I can generate using just the first name, using the rawValue.split method.  This way I can generate an e-mail that says Dear John, instead of Dear John Smith by pulling from that field.  The problem I have encountered there is that sometimes the person typing the name leaves a leading space before the name, so the e-mail gets generated with no name (so it says "Dear ,".  Any help is greatly appreciated.


This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Magus069

Alright, thanks for saying about the null value, your event is good, everything's fine.

Although, if you want to change the returned null value, here is the changes for returning nothing instead :

function trim(strValue){

    var str = new String(strValue);

    return strValue !== null ? str.replace(/(^\s*)|(\s*$)/g,"") : "";

}

Enjoy!

7 replies

Magus069
Level 10
March 22, 2016

Hi there,

here is a function which removes all spaces in the beginning and end of a string :

Hope this will help ya

Geckoz100
Geckoz100Author
Level 5
March 24, 2016

OK, that's great, thanks. Now, sorry I don't know how to read that regexp, can you explain how it works, so I know for the future?  And in what event do I put it?  I appreciate the help.

Magus069
Level 10
March 29, 2016

Yes sure,

This function can be called from a script object, so at anytime you want to call the trim function to retrieve a string with all spaces removed at the beginning and end of a string, feel free to call it.

Enjoy!

Geckoz100
Geckoz100Author
Level 5
March 29, 2016

OK, thanks for that explanation, very helpful.  So, I have one more question, sorry.  I'm not that experienced with javascript, so I don't know how to call the function.  Do I have to create a new script object and put your script in there?

Magus069
Level 10
March 29, 2016

Well either you insert the function within all events you need to call the trim function or you put it only once in a script object, which you can access from anywhere in the form by calling the script object then the function...


By putting the function within a script object, you only need this one occurrence to call the function anywhere in there form,

Otherwise if you insert the function within the click event of a button, the function is only accessible within that event

Geckoz100
Geckoz100Author
Level 5
March 30, 2016

It worked, thanks!  I haven't used many custom functions before, so I didn't know had to use the objName.functionName format to call it.  So I set it up like below, in the Exit event, because if I left it without the if statement and then typed something, exited the field, and came back to delete it, it would add "null" (in actual text) to the field.  Please let me know if you see anything I need to fix.


// remove leading and trailing spaces


if (this.rawValue != null) {


  this.rawValue = objSpaceTrim.trim(this.rawValue);


}



Magus069
Magus069Accepted solution
Level 10
March 31, 2016

Alright, thanks for saying about the null value, your event is good, everything's fine.

Although, if you want to change the returned null value, here is the changes for returning nothing instead :

function trim(strValue){

    var str = new String(strValue);

    return strValue !== null ? str.replace(/(^\s*)|(\s*$)/g,"") : "";

}

Enjoy!