Solved

trim leading and trailing spaces using jquery 1.3.2

Posted on 2010-08-18
8
729 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
3 Use Cases for Connected Systems

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

 

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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)

911 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

21 Experts available now in Live!

Get 1:1 Help Now