Solved

trim leading and trailing spaces using jquery 1.3.2

Posted on 2010-08-18
8
726 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
  • 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
This article discusses how to create an extensible mechanism for linked drop downs.
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)

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now