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
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
Identify, or convert? Two different things really.
If you want to identify:
If you want to convert:Code:if(/ /.test(value)) Do something here if there is a space in value
Which will strip all spaces from value before any further processing.Code:value = value.replace(/ /g, '');
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
You can use regex as John described above, or you can use a filter:Regex is more flexible, but the filter is generally faster, especially in small cases like this one.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"
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!
Code:function(v) {returnv !== ' '; }
Trinithis
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