Remove Leading And Trailing Whitespace From Zipcode And City Fields

Hi all, I would like to find a way to automatically remove leading and trailing whitespace from ZipCode of the Shipping Estimator and the Profile fields.

We often have reports form customers, that No Shipping Methods error message is shown when using the Shipping Estimator and in the Cart.

This problem occurs when a customer drags and drops the ZipCode from an external page (like an email or another webpage) to the shipping estimator Zipcode textarea.

Often, leading and trailing spaces or other whitespace are also in the string as its dropped in.

Is there a simple smarty command that automatically remove leading and trailing (only) whitespace?

For example in v2.1.4, the shipping_estimation.tpl template has this code:

<input type="text" class="input-text" id="{$prefix}elm_zipcode{$id_suffix}" name="customer_location[zipcode]" size="{if $location != "sidebox"}25{else}20{/if}" value="{$cart.user_data.s_zipcode}" />
```<br />
So the [b]$cart.user_data.s_zipcode[/b] variable will need to be trimmed somehow before the estimator sends the data to the Real-Time Shipping Calculator, in this case Australia Post. It may also effect Manual shipping methods.<br />
<br />
We do lose sales and get time consuming emails regarding this issue... any help will be greatly appreciated.

As for profile fields, I think that the cm-trim microformat can help you:


Hi eComLabs, thanks for the advice. Removing the trailing whitespace will be a start, but also need to ensure that there is no leading whitespace.

Ive taken a look at the microformats doc that you linked to and it states:

[quote]cm-trim - assigned to a label. Trailing spaces are removed from the value of the bound input during the validity check.[/quote]

So I changed the class of the label from:


(Deleted /cache and /compile folders to ensure the cache is cleared and cleared local browser cache)

However, there is no change to the operation of the shipping estimator, as the trailing whitespace is not removed.

The microformats doc is for v4 , and I see in changelog.txt that cm-trim was added in v2.2.1, so it wont be in the v2.1.4 Im trying to fix.

So Ill need a more “smarty” solution, since i also want to remove leading spaces

See the solution below


Yes, additional code customization is required for old CS-Cart versions. I missed this point

This works for trimming trailing spaces from a zipcode in v4 and I guess for any versions after v2.2.1.

At about line 95 of


Change this:

to this:

Thats for the zipcode in the shipping estimator.

Ive added the cm-trim microformat to core.js of our v2.1.4 store and it seems to work. It trims both leading and trailing whitespace from the zipcode. It will not trim spaces within the string, which is exactly what we want.

For v2.1.4, core.js file:

Find at about line 1802:

// Check the email field

replace it with"

// Check the need to trim value
if (lbl.hasClass('cm-trim')) {
// Check the email field

To trim the shipping estimator zipcode, edit /customer/views/checkout/components/shipping_estimation.tpl



Likewise any field should be able to be trimmed just by adding "cm-trim " to the class of the associated withn the same ID as the following .

So adding to /customer/views/profiles/components/profile_fields.tpl will trim all profile fields.


I think the CsCart architects need to add a “user-friendliness” section to the Job Spec that they hand to their programmers.

Also, should the description of “cm-trim” be corrected from:

[quote]cm-trim - assigned to a label. Trailing spaces are removed from the value of the bound input during the validity check.[/quote]


[quote]cm-trim - assigned to a label. Trailing and Leading spaces are removed from the value of the bound input during the validity check.[/quote]?

As it removes both, not just the trailing.

We glad to hear that you solved the issue.

Also, need to add cm-trim to the /customer/views/auth/login_form.tpl at about line 11 (v2.1.4)



This will trim the Sign-in and Returning Customer email address.

We had a customer today complain he could not log in to purchase. After a 30 minute phone call we discovered that he had pasted his email address with trailing spaces… So much time (and perhaps orders) lost because of such a simple lack of programming refinement.

In v4.3.4 we will add the trim of ZIP & other profile fields.

Thank you remote for you contribution.

Bug report:

Thanks you imac,that is fantastic news.

[quote name='imac' timestamp='1435125902' post='220154']

In v4.3.4 we will add the trim of ZIP & other profile fields.


Another place it will be handy is in the admin side, Tracking Number entry.

As when we drag n Drop a Tracking no fro Auspost eParcel, it always has trailing white space and sometimes leading tab chr's etc.

It does not actually cause a problem with Australia Post, but may be an issue with other carriers.