[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

trim leading and trailing spaces using jquery 1.3.2

Posted on 2010-08-18
8
Medium Priority
?
740 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

650 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