IT Community - Software Programming, Web Development and Technical Support

How to remove space appearing before the string in textbox?

This is a discussion on How to remove space appearing before the string in textbox? within the HTML, CSS and Javascript Coding Techniques forums, part of the Web Development category; The following codes in javascript helps to remove the space appearing before the text. <html> <head> &...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > HTML, CSS and Javascript Coding Techniques

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 01-23-2008, 01:10 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 188
poornima is on a distinguished road
Smile How to remove space appearing before the string in textbox?

The following codes in javascript helps to remove the space appearing before the text.
<html>
<head>
<script type="text/javascript">
function Validate()
{
var e1= document.getElementById("textbox1").value;
document.getElementById("textbox1").value = (e1.replace(/^\W+/,'')).replace(/\W+$/,'');
var ElemValue = document.getElementById("textbox1").value;
if(ElemValue.length==0)
{
alert("Enter Value");
}
if(ElemValue.length!=0)
{
alert("The space before "+ElemValue+" is removed");
}
}
</script>
</head>
<body>
<input type="text" id="textbox1"></input>
<input type="button" id="btn1" value="Click" onClick="Validate()"></input>
</body>
</html>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 01-23-2008, 02:42 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 303
Sathish Kumar is on a distinguished road
Exclamation Re: How to remove space appearing before the string in textbox?

Hi,
Dont you have Trim function in Javascript.Cant you use

var str=" Hello ";
str =str.Trim();
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 01-23-2008, 02:50 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 303
Sathish Kumar is on a distinguished road
Thumbs up Re: How to remove space appearing before the string in textbox?

Hi,
For trimming the string in all the sides,use the following code
Code:
function trimAll(sString) 
{ 
while (sString.substring(0,1) == ' ') 
   sString = sString.substring(1, sString.length); 

while (sString.substring(sString.length-1, sString.length) == ' ')  
   sString = sString.substring(0,sString.length-1);  
return sString; 
}
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 01-23-2008, 02:52 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 303
Sathish Kumar is on a distinguished road
Default Re: How to remove space appearing before the string in textbox?

Hi,
For trimming only the left part of the string ,you can use
Code:
function LeftTrim(var sString)
{
while (sString.substring(0,1) == ' ') 
   sString = sString.substring(1, sString.length); 

return sString ;
}
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 01-23-2008, 02:54 AM
Sathish Kumar Sathish Kumar is offline
D-Web Analyst
 
Join Date: Feb 2007
Posts: 303
Sathish Kumar is on a distinguished road
Thumbs up Re: How to remove space appearing before the string in textbox?

Hi,
For trimming only the right part of the string ,you can use
Code:
function RightTrim(var sString)
{
while (sString.substring(sString.length-1, sString.length) == ' ')  
   sString = sString.substring(0,sString.length-1);  
return sString;
}
__________________
Sathish Kumar.R
Knowledge is meant to SHARE
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 01-23-2008, 03:03 AM
poornima poornima is offline
D-Web Sr.Programmer
 
Join Date: Dec 2007
Posts: 188
poornima is on a distinguished road
Smile Re: How to remove space appearing before the string in textbox?

Hi,
The Substring you have used works fine.But the Trim() doesn't work in JS.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 03-24-2009, 06:14 AM
leveup2 leveup2 is offline
D-Web Trainee
 
Join Date: Mar 2009
Posts: 8
leveup2 is on a distinguished road
Default Re: How to remove space appearing before the string in textbox?

I have the same problem and now it has been sorted!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 03-25-2009, 10:28 AM
mD1gCsjy mD1gCsjy is offline
D-Web Trainee
 
Join Date: Mar 2009
Posts: 2
mD1gCsjy is on a distinguished road
Default Re: How to remove space appearing before the string in textbox?

Hm, I agree, but here is an opinion

Last edited by arjkhanna : 06-23-2009 at 07:47 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 06-18-2009, 01:30 AM
Kalimai Kalimai is offline
D-Web Trainee
 
Join Date: Jun 2009
Posts: 2
Kalimai is on a distinguished road
Default Re: How to remove space appearing before the string in textbox?

i need to remove all text from a text box that comes before the ":" and remove .... I have to type in several numbers before they start to appear from the right. ... What code would I use to go down the column and remove the spaces beween the ... how would I remove any text in a string that is between
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
display xml in textbox arjkhanna C# Programming 4 05-28-2009 08:28 PM
Showing C# Intellisense in TextBox arjkhanna C# Programming 3 05-25-2009 10:56 PM
textbox in Gridview packirisamy ASP and ASP.NET Programming 2 07-22-2008 03:56 AM
What is diffrenece between string.compare and string.compareordinal shaalini ASP and ASP.NET Programming 3 12-28-2007 09:46 PM
What's the easiest way to remove characters from the end of a string in php? oxygen PHP Programming 1 07-26-2007 01:53 AM


All times are GMT -7. The time now is 08:22 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0