Here's my version of a trim function as well as a few others ;)

I would put the "trimSpaces" function in an includes .js file and call it in
your pages using:

function validate()
{
     // strips leading and trailing spaces
     for (i=0; i < document.form.elements.length; i++) 
     {
        document.form.elements[i].value = trimSpaces(document.form.elements[i].value);
     }
     // Now do field validation
}

// This function is for stripping leading and trailing spaces
function trimSpaces(str) 
{ 
        if (str != null) 
        {
                var i; 

                for (i=0; i < str.length; i++) 
                {
                        if (str.charAt(i)!=" ") 
                {
                                str=str.substring(i,str.length); 
                                break;
                        } 
                } 

                for (i = str.length-1; i >= 0; i--) 
                {
                        if (str.charAt(i)!=" ") 
                {
                                str = str.substring(0,i+1); 
                                break;
                        } 
                } 

                if (str.charAt(0)==" ") 
                {
                        return ""; 
                }               
                else 
                {
                        return str; 
                }
        }
} 

Hope this helps!

Matt

--- Thinh Doan <[EMAIL PROTECTED]> wrote:
> Thank you very much Matt.  Yes I'd like to get the "trim" fct or anything
> else in your arsenal that we'd be willing to share :-).
> 
> Best regards,
> 
> Thinh
> 
> -----Original Message-----
> From: Matt Raible [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, October 18, 2001 8:05 PM
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: Re: How to stop form submit on javascript error?
> 
> 
> Change your javascript code to the following:
>  <script>
>   function validate()
>  {
>       if (document.forms[0].username.value == "")
>       {
>               alert("User Name is a required field. Please fill in your User Name.");
>               document.forms[0].focus();
>               document.forms[0].select();
>               return false;
>       }
>         else
>         {
>               return true;
>         }
>  }
>  </script>
> 
> Just for good javascript practice, you might want to check for a space as
> the
> first character as well.  I have a "trim" function in my javascript arsenal
> that I call before I do checking for any specific fields - this trims white
> space.  Let me know if you're interested, and I can send the function.
> 
> Matt
> 
> --- Thinh Doan <[EMAIL PROTECTED]> wrote:
> > How do you stop the form from being submitted if a javascript error, which
> > was called from the <html:form onsubmit...>,  occurs. I get an error and I
> > say ok and the javascript tries to put focus on the field which is causing
> > the error, but the form still submits to the action class.
> >
> > Here is some code:
> > <script>
> >  function validate()
> > {
> >     if (document.forms[0].username.value == "")
> >     {
> >             alert("User Name is a required field. Please fill in your User Name.");
> >             document.forms[0].focus();
> >             document.forms[0].select();
> >             return false;
> >     }
> >     return true;
> > }
> > </script>
> >
> > <html:form action="jsp/change_password.do" focus="username"
> onsubmit="return
> > validate();">
> > ....
> >
> >
> > Thanks for your time.
> >
> > Thinh
> >
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Make a great connection at Yahoo! Personals.
> http://personals.yahoo.com
> 


__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com

Reply via email to