View Full Version : Javascript function to identify space in a value
sharatcg
10-23-2008, 03:40 AM
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
jscheuer1
10-23-2008, 04:27 AM
Identify, or convert? Two different things really.
If you want to identify:
if(/ /.test(value))
Do something here if there is a space in value
If you want to convert:
value = value.replace(/ /g, '');
Which will strip all spaces from value before any further processing.
You can use regex as John described above, or you can use a filter:
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.
Trinithis
10-24-2008, 12:08 AM
function(v) { return v !== ' '; }
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.