Solved

trim leading and trailing spaces using jquery 1.3.2

Posted on 2010-08-18
8
733 Views
Last Modified: 2012-05-10
jQuery.trim(value) doesn't work with jquery 1.3.2, any suggestions on how to trim leading and trailing spaces from a variable?
0
Comment
Question by:cgray1223
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33470937
You've the answer here : http://api.jquery.com/jQuery.trim/

(check the comments)
0
 

Author Comment

by:cgray1223
ID: 33470995
I tried that and I get a object doesn't support property error.  I'm using jquery 1.3.2.

var firstname = $("#firstname"),      
firstname = jQuery.trim(firstname);
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33471012
What about :

$("#firstname").text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');

or

$("#firstname").val().replace(/(^\s+|\s+$)/g, '');
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 82

Expert Comment

by:leakim971
ID: 33471022
test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		var fn1 = $("#firstname1").val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');
		$("#firstname1").val( fn1 );

		var fn2 = $("#firstname2").val().replace(/(^\s+|\s+$)/g, '');
		$("#firstname2").val( fn2 );
	});
</script>
</head>
<body>
<input type="text" value="              test1                         " id="firstname1" />
<input type="text" value="              test2                         " id="firstname2" />
</body>
</html>

Open in new window

0
 

Author Comment

by:cgray1223
ID: 33471089
I couldn't get that to work either, maybe the below will shed some light...

attempt #1:
$(document).ready(function() {      
var firstname = $("#firstname");
//var firstname = $("#firstname").text().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, ''); //tried this
//var firstname = $("#firstname").val().replace(/(^\s+|\s+$)/g, ''); //tried this
});

attempt#2:
$(document).ready(function() {      
var firstname = $("#firstname");
var bValid = checkRegexp(firstname,/^([0-9a-zA-Z])+$/,"First Name may consist of a-z 0-9");
function checkRegexp(o,regexp,n) {
                  //o = o.val().replace(/(^\s+|\s+$)/g, ''); //tried this
                        //o = o.text().replace(/(^\s+|\s+$)/g, ''); //tried this
                  if ( !( regexp.test( o.val() ) ) ) {
                        o.addClass('ui-state-error');
                        updateTips(n);
                        return false;
                  } else {
                        return true;
                  }
            }
});
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33471097
Try :

$("#firstname").val(        $("#firstname").val().replace(/(^\s+|\s+$)/g, '')            );
0
 

Author Comment

by:cgray1223
ID: 33471171
That did work once I moved it to the open of the dialog function, so your other solutions probably would have worked as well.  Thanks!  
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33471174
You're welcome! Thanks for the points!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question