Solved

trim string in javascript function

Posted on 2008-10-17
7
332 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

747 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

12 Experts available now in Live!

Get 1:1 Help Now