Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    79
    Thanks
    1
    Thanked 0 Times in 0 Posts

    deleting white spaces

    How can I to delete white spaces around parameter that I to pass with HTML forms.
    For example, if I have 'firstname lastname ' or ' firstname lastname', exist it a builtin function in javascript to obtian 'firstname lastname'?

    savio

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may use a trim function (based on Regular Expressions)
    Code:
    function alltrim(str) {
    return str.replace(/^\s+|\s+$/g, '');
    }
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    New versions of Safari, Chrome , Opera and Firefox have a native string trim,
    and IE9 does, as well. You could substitute Kor's method, if trim is not already defined

    Code:
    if(!String.prototype.trim)String.prototype.trim=function(){
    	return this.replace(/^\s+|\s+$/g,'');
    }


  •  

    LinkBacks (?)


    Posting Permissions

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