Link to home
Start Free TrialLog in
Avatar of robrodp
robrodpFlag for Mexico

asked on

trim string in javascript function

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?
Avatar of maXXXeE
maXXXeE

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
Avatar of robrodp

ASKER

Can you post full syntax (function, etc) and how to pass it to my validation function
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);
Avatar of robrodp

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of maXXXeE
maXXXeE

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of robrodp

ASKER

Let me work on it BBK