views:

89

answers:

3

Hi,

I've searched around for this a lot and can't find a specific example of what I'm trying to do. Basically I want to get the value of a textbox, by the name of the text box (not id). Then I want to remove ALL spaces, not just leading or trailing, but spaces in the text too. For example for this html code:

<INPUT style="TEXT-ALIGN: right;" onkeyup="fieldEdit();" onchange="fieldChange();" NAME="10010input" VALUE="4 376,73" readonly >

I have this javascript:

var val = document.CashSheet.elements["10010input"].value; 
val2 = val.replace(<someregex>, '');
alert(val2);

But I've tried many available regex expressions that remove all spaces, but all only seem to remove leading and trailing spaces, for example the value above 4 376,73 should read as 4376,73 in the alert above but it doesn't. After looking into this normal regex for removing spacesfrom a text field contents is not working and I believe its because its being populated in Polish localised settings on the web server. What \u char/regex exp do I need to capture for a "Polish space" so to speak, in ascii it comes up as 20 but the regex exps that most people are suggesting for spaces does not capture it.

+1  A: 

You can use document.getElementsByName to get hold of the element without needing to go through the form, so long as no no other element in the page has the same name. To replace all the spaces, just use a regular expression with the global flag set in the element value's replace() method:

var el = document.getElementsByName("10010input")[0];
var val = el.value.replace(/\s/g, "");
alert(val);
Tim Down
Hi Tim, quick question, I am trying what you're suggesting for a text field contents but its not working and I believe its because its being populated in Polish localised settings on the web server. What \u char do I need to capture for a "Polish space" so to speak, in ascii it comes up as 20 but the regex does not capture it, but will capture other spaces I put in a string myself.
TMS
Really? Are you sure that's not 20 in hex (the normal space character is 20 in hex)? If you need to specify a list of space characters to match you could do something like `new RegExp("[\\s\u1234\u4321]", "g")` instead of `/\s/`.
Tim Down
Yep, its coming back like "4 376", if I try that regex on it, it doesn't do anything but if i add "a a" to the end of that string the regex will make it output like this: "4 376aa" so it works on my space but not on the space coming back from the Polish web server. I think its a different Unicode space character, which I will try to find which one it is then use you new RegExp call.
TMS
Ok i found its \u00A0 , so a nbsp, but need to find a regex that matches that now as new RegExp("[\\s\u00A0]", "g") doesn't work :(
TMS
Really? `new RegExp("[\\s\u00A0]", "g").test("\u00A0");` works for me, including in IE (in which `\s` doesn't match `\u00A0`, violating the ECMAScritp spec).
Tim Down
This did it! .replace([\f\n\r\t\v\u00A0\u2028\u2029], '_')
TMS
+3  A: 

You need to "generalize" that regexp you're using so it's applied to all matches instead of just the first. Like this:

val = val.replace(/\s/g, '')

Notice the 'g' that modifies the regexp so it becomes "general".

Abel Tamayo
+1 for `\s` character class; I'd probably use the work "global" instead of "general"
Michael Haren
@Michael: Why is `\s` significantly better than just a space character here? There can't be any line break characters in an `<input>` and non-breaking spaces are not commonly typed into a text box.
Tim Down
@Michael Haren: I knew it started with a 'g', but I didn't know which one it was exactly.@Tim Down: I think \s is more readable, isn't it?
Abel Tamayo
@Abel: Dunno. Quite subjective, I'd say. It's not a big issue, and `\s` is probably the better option functionality-wise.
Tim Down
@Tim There are dozens of whitespace characters that could be pasted in that aren't a simple "space"
Michael Haren
@Michael: Not many are covered by `\s` though. It matches precisely nine characters (\u0009, \u000A, \u000B, \u000C, \u000D, \u0020, \u00A0, \u2028 and \u2029). However, I concede the point: `\s` is the better option.
Tim Down
@Tim Good to know, thanks!
Michael Haren
Hi all thanks for the replies, quick question at Tims comment, I am trying what you're suggesting for a text field contents but its not working and I believe its because its being populated in Polish localised settings on the web server. What \u char do I need to capture for a "Polish space" so to speak, in ascii it comes up as 20 but the regex does not capture it, but will capture other spaces I put in a string myself.
TMS
Some browsers do allow pasting CR/LF into in `<input>` elements.
eyelidlessness
@eyelidlessness: Yes. My phrasing was bad in an attempt to keep things simple.
Tim Down
A: 

you can use this jQuery method http://api.jquery.com/jQuery.trim/
val = $.trim(val); OR val = jQuery.trim(val);

Space Cracker
This only removes leading and trailing spaces and not Polish Unicode spaces inside a string :(
TMS