Expand my Community achievements bar.

Remove spaces from a text field

Avatar

Level 7

I need to remove spaces from a text field in case the end user uses the space bar

to erase text. I found this script on the Forum:

function RLTrim(Cadena){
var str = new String(Cadena);
return str.replace(/(^\s*)|(\s*$)/g,"");
}

I already have JavaScript in the Exit event on this field. I'm not certain where I should put this new script.

Thanks,

MDawn

6 Replies

Avatar

Level 10

Hi,

Not sure if this is much use to you, as it will not allow the user to use the space bar at all ???

https://acrobat.com/#d=7jsiacYhcjNRgDCabY2cuw

Niall

Avatar

Level 10

Thinking about it, testing for the length of the newText will allow you to only reset the field if the space bar is the first character.

Updated sample here: https://acrobat.com/#d=7jsiacYhcjNRgDCabY2cuw

Niall

Avatar

Level 10

You can place it in the exit event of the field..Like..

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

Thanks

Srini

Avatar

Level 7

I don't think this isn't exactly what I need. I should have explained in more detail.

We have text fields that when they have text in them then require that there be a value in a drop-down field. If there is text and no value in the drop-down field, there is an error message. If the user erases the text, the script was still thinking there was text in the field.

A JavaScripter here at work helped me add lines that will check the text field if the user has entered data then erased the data so the form will see the field as empty/null. However, if the user uses the space bar to clear the text from the field, obviously, the form still sees characters in the field and throws the error message. So we need to be able to strip the spaces from the field and then treat the field as empty.

Will your script address this? If so, what event do I put the script in?

Thanks,

Margaret Dawn

Supervisor - Documentation

W: 630-850-1065

Avatar

Level 10

You have two options to do this..

1) Do not allow the user to use space bar to delete the text. That way you can make sure you will never have spaces created by spacebar.

    You usually do this in the change event.

    Niall has provided you the sample in the above response on how to do this.

2) Upon exiting the text field, you can remove any spaces in the front and back of the text in the TextField. For whcih I gave you the line of script you can use in the Exit event of the TextField.

    If you have more fields that need the similar requirement, it is advisable to put the script in a script object and make a call to the function in the exit event.

Hope this helps.

Thanks

Srini

Avatar

Level 10

Hi,

I have put an app.alert in the exit event to show that when the user uses the space bar to delete the content it resets the value to null.

Same URL: https://acrobat.com/#d=7jsiacYhcjNRgDCabY2cuw

The script is in the change event and will reset the field if the field contains a single whitespace character. This is equivalent to a user selecting all of the text and hitting the space bar.

I have a second example which fires on exit event.

Lastly I have a third field that doesn't have any script, so that if the user uses the spacebar to delete text, it will leave a whitespace character.

Hope that helps,

Niall