Solved

trim string in javascript function

Posted on 2008-10-17
7
336 Views
Last Modified: 2008-10-18
I have this javascript functionin an asp script  to validate some data:

 if (document.Form1.Msg.value.length == 0) {
                alert("Falta Mensaje")
                return(false)
           }
If the Msg has spaces it validates ok. How can I run the function trimming left and right spaces?
0
Comment
Question by:robrodp
[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
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:maXXXeE
ID: 22741256
while (str.substring(0,1) == ' ')
{
str = str.substring(1, str.length);
}
while (str.substring(str.length-1, str.length) == ' ')
{
str = str.substring(0,str.length-1);
}

str -> your string with spaces
0
 

Author Comment

by:robrodp
ID: 22741323
Can you post full syntax (function, etc) and how to pass it to my validation function
0
 
LVL 7

Expert Comment

by:maXXXeE
ID: 22741389
function strim(str)
{
while (str.substring(0,1) == ' ')
{
str = str.substring(1, str.length);
}
while (str.substring(str.length-1, str.length) == ' ')
{
str = str.substring(0,str.length-1);
}
return str;
}


you can call the function just like any other function call

trimmedstring=strim(originalstring);
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:robrodp
ID: 22741462
I define the function fine...

Where does it fin in:

if (document.Form1.Msg.value.length == 0) {
                alert("Falta Mensaje")
                return(false)

So that the Msg variable is trimmed?
0
 
LVL 7

Accepted Solution

by:
maXXXeE earned 500 total points
ID: 22741593
see, i dont know what all your form contains and what all u are trying to do.

say Msg is the name of the textbox, in ur main function u assign the value to a variable named "originalstring"


function strim(str)   // function for trimming text
{
while (str.substring(0,1) == ' ')
{
str = str.substring(1, str.length);
}
while (str.substring(str.length-1, str.length) == ' ')
{
str = str.substring(0,str.length-1);
}
return str;
}


function youroriginalfunction
{
...//all the code

var originalstring=document.Form1.Msg.value; //the value of textbox is in originalstring variable

var trimmedstring=strim(originalstring);//here you get the trimmed output in variable called trimmedstring

...///rest of the code
}

0
 
LVL 7

Expert Comment

by:maXXXeE
ID: 22741636
i can help u if you send the full function and what u are trying to do with the function. Part of the function alone (like the one u have given) does not make sense
0
 

Author Comment

by:robrodp
ID: 22741878
Let me work on it BBK
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
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…

726 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