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

0

6 Answers 6

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

8 Comments

My string or the input string?
@user3135730 the string that you want to remove the whitespace before and after
Might want to mention that this requires jQuery.
@ScottGress i assumed he already has it because he tagged jQuery :P
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
|
19

Use the trim() method

Example:

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

This will output :

Hello World!

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

Comments

5

You may use the trim() method.

1 Comment

support isn't in all browsers - but that's just mostly IE8 and lower
3

Do this for cross-browser:

var x = $("#clueOneInput").find('input[type=text]').val().replace(/^\s+|\s+$/g,"");

2 Comments

Amit I'm not sure I understood what you meant by your comment. Can you expand?
@user3135730, I meant this was a more reliable solution.
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.

Comments

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')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.