Close

Results 1 to 6 of 6
  1. #1
    VIP Member CzarJunkie's Avatar
    Join Date
    Jun 2001
    Location
    Atlantis
    Posts
    13,754
    Thanks
    832
    Thanked:        3,225
    Karma Level
    1993

    Default Javascript help, removing leading white space

    Using the following code to remove the first leading white space in a form field, can someone give me a clue as to how I can remove more than one space. Thanks

    PHP Code:
    var document.form.field
    var a.value.replace(/^ /,"");
    a.value b

  2. #2
    VIP Member CzarJunkie's Avatar
    Join Date
    Jun 2001
    Location
    Atlantis
    Posts
    13,754
    Thanks
    832
    Thanked:        3,225
    Karma Level
    1993

    Default Re: Javascript help, removing leading white space

    found it, cheers

  3. #3
    DF VIP Member BertRoot's Avatar
    Join Date
    Sep 2004
    Location
    Poppy Fields
    Posts
    23,955
    Thanks
    1,038
    Thanked:        2,059
    Karma Level
    2514

    Default Re: Javascript help, removing leading white space

    Quote Originally Posted by CzarJunkie
    Using the following code to remove the first leading white space in a form field, can someone give me a clue as to how I can remove more than one space. Thanks

    PHP Code:
    var document.form.field
    var a.value.replace(/^ /,"");
    a.value b
    You could use compressWhiteSpace to get it down to 1 and then run the above.

    EDIT:You found it then.


  4. #4
    VIP Member CzarJunkie's Avatar
    Join Date
    Jun 2001
    Location
    Atlantis
    Posts
    13,754
    Thanks
    832
    Thanked:        3,225
    Karma Level
    1993

    Default Re: Javascript help, removing leading white space

    Thanks m8, this does the trick

    PHP Code:

    var document.form.field
    var a.value.replace(/^\s+|\s+$/g"");
    a.value b

  5. #5
    DF VIP Member BertRoot's Avatar
    Join Date
    Sep 2004
    Location
    Poppy Fields
    Posts
    23,955
    Thanks
    1,038
    Thanked:        2,059
    Karma Level
    2514

    Default Re: Javascript help, removing leading white space

    If you wanna strip white space off the front and end my mate supplies this to his customers.


    PHP Code:
    <!-- THREE STEPS TO INSTALL REMOVE_XS_WHITESPACE:

      
    1.  Copy the coding into the HEAD of your HTML document
      2.  Add the onLoad event handler into the BODY tag
      3.  Put the last coding into the BODY of your HTML document  
    -->

    <!-- 
    STEP ONEPaste this code into the HEAD of your HTML document  -->

    <
    HEAD>

    <
    SCRIPT LANGUAGE="JavaScript">
    <!-- 
    Begin
    function remove_XS_whitespace(item)
    {
      var 
    tmp "";
      var 
    item_length item.value.length;
      var 
    item_length_minus_1 item.value.length 1;
      for (
    index 0index item_lengthindex++)
      {
        if (
    item.value.charAt(index) != ' ')
        {
          
    tmp += item.value.charAt(index);
        }
        else
        {
          if (
    tmp.length 0)
          {
            if (
    item.value.charAt(index+1) != ' ' && index != item_length_minus_1)
            {
              
    tmp += item.value.charAt(index);
            }
          }
        }
      }
      
    item.value tmp;
    }
    //  End -->
    </script>

    </
    HEAD>

    <!-- 
    STEP TWOInsert the onLoad event handler into your BODY tag  -->

    <
    BODY onLoad="mystring.select()">

    <!-- 
    STEP THREECopy this code into the BODY of your HTML document  -->

    <
    DIV ALIGN="CENTER">
    <
    B>String to strip excess spaces from:</B>
    </
    DIV>
    <
    DIV ALIGN="CENTER">
    <
    INPUT TYPE="text" NAME="mystring" VALUE="  This  is  a  string  " SIZE="50" MAXLENGTH="50">
    </
    DIV><BR>
    <
    DIV ALIGN="CENTER">
    <
    INPUT TYPE="SUBMIT" NAME="submit" VALUE="Strip XS Spaces" ONCLICK="remove_XS_whitespace(mystring);mystring.select();return false">
    </
    DIV>
    <
    p>
    <
    DIV ALIGN="CENTER">
    Try <
    A HREF="" ONCLICK="mystring.value='        ';mystring.select();return false;">8 blank spaces</A><BR>
    or <
    A HREF="" ONCLICK="mystring.value='        This    is      a    string        ';mystring.select();return false;">try this string</A><BR>
    or <
    A HREF="" ONCLICK="mystring.value='          1234567890          1234567890          ';mystring.select();return false;">try this string</A>
    </
    DIV>
    </
    BODY


  6. #6
    DF MaSter bananas33's Avatar
    Join Date
    Nov 2003
    Location
    e lancs
    Posts
    64
    Thanks
    0
    Thanked:        0
    Karma Level
    252

    Default Re: Javascript help, removing leading white space

    I use this:

    PHP Code:
    String.prototype.trim = function()
    {
        return 
    this.replace(/(^\s*)|(\s*$)/g"");

    which then allows you to use things like field.value.trim()

Similar Threads

  1. removing tar!
    By EUPHORiA in forum Cars & Motorbikes
    Replies: 6
    Last Post: 14th September 2002, 09:21 PM
  2. PS1 BOOTS IN BLACK AND WHITE can u help ???
    By SKABOO in forum Sony Consoles
    Replies: 6
    Last Post: 9th September 2002, 12:14 AM
  3. Sapphire Radeon 9700PRO 128MB DDR DVI AGP TV-Out White Boxed
    By brainz2002 in forum Cheapskates Corner
    Replies: 3
    Last Post: 7th September 2002, 06:10 PM
  4. Removing BIOS password from Sony V910
    By Tattooed Freak in forum PC Problems
    Replies: 3
    Last Post: 2nd September 2002, 09:09 PM

Social Networking Bookmarks

Posting Permissions

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