Solved

trim leading and trailing spaces using jquery 1.3.2

Posted on 2010-08-18
8
735 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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.
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)

724 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