Solved

trim string in javascript function

Posted on 2008-10-17
7
335 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

856 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