Results 1 to 4 of 4

Thread: Javascript-Trim functions

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2001
    Location
    Brooklyn, NYC
    Posts
    112
    Can any one tell me,what are the equivalent functions in Javascript for trim,rtrim ,and ltrim.

  2. #2
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    There's no JavaScript trim() function as such. This should suit your needs though....



    Code:
    <script language="JavaScript">
    <!-- 
    function trim(strText) {
        	// this will get rid of leading spaces
        	while (strText.substring(0,1) == ' ')
            	strText = strText.substring(1, strText.length);     
    	
    	// this will get rid of trailing spaces
        	while (strText.substring(strText.length-1,strText.length) == ' ')
            	strText = strText.substring(0, strText.length-1);
        return strText;
    }
     //--></script>
    The script came from here: http://developer.irt.org/script/1310.htm
    Mark
    -------------------

  3. #3
    Hyperactive Member
    Join Date
    Jun 2004
    Posts
    369

    Re: Javascript-Trim functions

    Hi, using the above trim function why is this not working ?

    Code:
    <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
    	
        function ValidateForm(form)
        {	
             //This line works
             alert ('*' + form.txtCourriel.value);
    
             // This line does not work 
             alert ('*' + trim(form.txtCourriel.value));
             // This line does not work 
             alert ('*' + trim(form.txtCourriel));
        } 
    			
       </SCRIPT>
    How do I use that trim function ?

  4. #4
    Addicted Member Guru's Avatar
    Join Date
    May 2000
    Location
    sulking in the cupboard under the stairs
    Posts
    237

    Re: Javascript-Trim functions

    Jeeze that's an old thread!
    Code:
    alert ('*' + trim(form.txtCourriel.value));
    This is correct and it works fine for me.
    Another light-hearted post from Guru

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width