Expand my Community achievements bar.

SOLVED

Trimming whitespace from input fields

Avatar

Level 2

I'm finding that some of my script logic is failing when an end-user happens to deposit some whitespace into an optional input field. In other words, if the user hits the space bar while an input field has the cursor, then moves on, it is no longer a null field, and my scripts will expect data to be there (but the data is whitespace).

What is the most straightforward way to trim whitespace (or eliminate a string that's nothing but space) from any and all input fields? Thanks for reading.

1 Accepted Solution

Avatar

Correct answer by
Level 6

Simple way is to add the following javascript to the exit: event of the input field:

this.rawValue

= this.rawValue.replace(/^\s+|\s+$/g,"");

Whenever a user fills in and leaves the field it will trim the spaces (you will see this happen on the screen).

View solution in original post

2 Replies

Avatar

Correct answer by
Level 6

Simple way is to add the following javascript to the exit: event of the input field:

this.rawValue

= this.rawValue.replace(/^\s+|\s+$/g,"");

Whenever a user fills in and leaves the field it will trim the spaces (you will see this happen on the screen).

Avatar

Level 2

Works perfectly, and great insight into a function I did not know existed. Thanks.