JavaScript to remove leading & trailing white spaces and successive spaces in a String field.

Posted: March 28, 2013 in JQuery
Tags: , , , , ,

Recently I was working on primefaces and happen to come across this scenario,
where I had to remove all the leading & trailing white spaces along with successive spaces
from a string field.

I guess you guys are here for the below script. ENJOY !!

<h:outputLabel>Vehicle Make<span>*</span></h:outputLabel>
<p:inputText id=”veh_make” value=”#{vinBean.make}” maxlength=”50″ onchange=”trim(this)”>

<script language=”javascript”>
function trim(inputText) {
inputText.value = inputText.value.replace(/\s+/g,’ ‘).replace(/^\s+|\s+$/,”);

  1. adithya says:

    /^\s+|\s+$/g, ”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s