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

1
  • 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, ''). Commented Apr 11, 2011 at 22:10

2 Answers 2

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, ''));
Sign up to request clarification or add additional context in comments.

1 Comment

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
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, ''); 
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.