0

Hi following code throws the "Object doesn't support this property or method" error in IE.. var value = $input.val().replace(/ /g, '').trim(); It is part of form validation, in every other browser is it working well, but in IE it doesnt..please help

simekadam
  • 7,334
  • 11
  • 56
  • 79
  • 1
    `trim()` was introduced with ECMAScript 5 and IE8 does not support this. Actually it does not seem you need `trime` anyway, because you already remove every space by calling `replace(/ /g, '')`. If you are worried about the other whitespace characters, you could use `\s`: `$input.val().replace(/\s+/g, '')`. – Felix Kling Apr 11 '11 at 22:10

2 Answers2

3

As was commented on your question, IE8 doesn't support trim(). jQuery includes the trim() function, so I'd use that:

var value = jQuery.trim($input.val().replace(/ /g, ''));
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Sorry, I can only accept only one answer:)But thanks, I'd read the api and not mindless autowrite the code:DI thought I was using trim form jQuery..Everything clear already – simekadam Apr 11 '11 at 22:25
2

From Ben Rowe: Add the following code to add trim functionality to the string.

if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}
Community
  • 1
  • 1
rebelliard
  • 9,592
  • 6
  • 47
  • 80