?
Solved

trim string in javascript function

Posted on 2008-10-17
7
Medium Priority
?
338 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

777 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