Results 1 to 5 of 5

Thread: Javascript function to identify space in a value

  1. #1
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Javascript function to identify space in a value

    Hello All,

    I want to identify if there is a space in a field value. For e.g. '0 ' is not same as '0'. I want to convert this '0 ' to '0' to do some validations. How is it possible in javascript?

    Thanks & Regards,

    Sharat

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Identify, or convert? Two different things really.

    If you want to identify:

    Code:
    if(/ /.test(value))
    Do something here if there is a space in value
    If you want to convert:

    Code:
    value = value.replace(/ /g, '');
    Which will strip all spaces from value before any further processing.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You can use regex as John described above, or you can use a filter:
    Code:
    var Functional = {
      filter: function(p, a) {
        for (var i = 0, n = a.length, r = []; i < n; ++i)
          if (p(a[i], i))
            r[r.length] = a[i];
    
        return r;
      }
    };
    
    function unspace(s) {
      return Functional.filter(function(v) { return v !== ' '; }, s.split("")).join("");
    }
    
    unspace(" 0 0 "); // "00"
    Regex is more flexible, but the filter is generally faster, especially in small cases like this one.
    Last edited by Twey; 10-24-2008 at 05:37 AM. Reason: Fix error.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    function(v) { return v !== ' '; }
    Trinithis

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oops, yes. Indeed.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •