11
var $one = $('#oneClueShow');
    var x = $("#clueOneInput").find('input[type=text]').val();
    if(x == 'D R' || x == 'd r'|| x == 'D r'|| x == 'd R'|| x == 'd'|| x == 'r' || x == 'd R' || x == 'T A')

Above is a snippet of java/ I have. What it does is takes an input -- then checks for a match. The bug I'm looking to resolve is if there are spaces after or before it isn't recognized.

For example within the input someone can type 'D R' no problem. If a space is added like so 'D R ' then it no longer recognizes the input.

Is there something I can do to affect the input to ensure no space before/after prior to button click? I've been looking into replace, but can't seem to get it going.

A reference for what I've been looking at : http://www.w3schools.com/jsref/jsref_replace.asp

user3135730
  • 320
  • 1
  • 3
  • 8

6 Answers6

22

You can use $.trim

$.trim(yourstring)

which will allow for cross browser compatibility

In your case it would be

var x = $.trim($("#clueOneInput").find('input[type=text]').val());
wirey00
  • 33,517
  • 7
  • 54
  • 65
  • My string or the input string? – user3135730 May 06 '14 at 02:14
  • @user3135730 the string that you want to remove the whitespace before and after – wirey00 May 06 '14 at 02:15
  • 1
    Might want to mention that this requires [jQuery](http://jquery.com). – sgress454 May 06 '14 at 02:16
  • @ScottGress i assumed he already has it because he tagged jQuery :P – wirey00 May 06 '14 at 02:16
  • I do have it already. I'm just trying to figure out the trim relationship here. In relation to at which point a trim would need to take place. At the input or below is what I'm trying to figure out I think. Thanks for this info. D R ect are my set variables and not the input. If that makes sense – user3135730 May 06 '14 at 02:19
  • @user3135730 I updated my answer to show you where to trim - you would want to trim it there so you don't have to trim inside the if statement – wirey00 May 06 '14 at 02:20
  • @ᾠῗᵲᄐᶌ it looks as if ie8 is working. I haven't found any reference to say otherwise. Is this something that you have experienced before? – user3135730 May 06 '14 at 02:38
  • nope - it should work with no problem – wirey00 May 06 '14 at 02:45
17

Use the trim() method

Example:

var str = "       Hello World!        ";
alert(str.trim());

This will output :

Hello World!

var str = "       Hello World!        ";
alert(str.trim());
Rifky Niyas
  • 1,737
  • 10
  • 25
5

You may use the trim() method.

antoyo
  • 11,097
  • 7
  • 51
  • 82
3

Do this for cross-browser:

var x = $("#clueOneInput").find('input[type=text]').val().replace(/^\s+|\s+$/g,"");
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
3

You can do a check before-hand:

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.toString().replace(/^\s+|\s+$/g, '');
  };
}

Will enable .trim in non-supported browsers. And then, you can use it like:

 var x = $("#clueOneInput").find('input[type=text]').val().trim();

With the benefit that you can use this .trim method at many other places in the code too.

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
1

You should always use $.trim() while comparing the string. You can use $.trim() like:

  if($.trim(x) == 'D R' || $.trim(x) == 'd r'|| $.trim(x) == 'D r'|| $.trim(x) == 'd R'|| $.trim(x) == 'd'|| $.trim(x) == 'r' || $.trim(x) == 'd R' || $.trim(x) == 'T A')
Naveen Chandra Tiwari
  • 5,055
  • 3
  • 20
  • 26