56

I am trying to detect if a user enter whitespace in a textbox:

 var regex = "^\s+$" ; 
 if($("#siren").val().match(regex)) {
     echo($("#siren").val());
     error+=1;
    $("#siren").addClass("error");
    $(".div-error").append("- Champ Siren/Siret ne doit pas etre vide<br/>");
 }

if($("#siren").val().match(regex)) is supposed to match whitespace string, however, it doesn' t seems to work, what am I doing wrong ?

Thanks for your helps

1
  • 1
    When you say "doesn't seems to work", could you be more specific? Are there any example inputs that fail that you could share? Commented Oct 1, 2013 at 16:36

6 Answers 6

73

The \ (backslash) in the .match call is not properly escaped. It would be easier to use a regex literal though. Either will work:

var regex = "^\\s+$";
var regex = /^\s+$/;

Also note that + will require at least one space. You may want to use *.

Sign up to request clarification or add additional context in comments.

1 Comment

I use var regex = "^\\s+$"; it works like a charm, thank you very much all of you
38

If you are looking for empty string in addition to whitespace you meed to use * rather than +

var regex = /^\s*$/ ;
                ^

Comments

24

If you're using jQuery, you have .trim().

if ($("#siren").val().trim() == "") {
  // it's empty
}

7 Comments

If you only need to support JavaScript 1.8 browsers, there is String.trim
@ExplosionPills I'm sure that jQuery does this internally, and OP already uses jQuery.
@Overcode where do you see simplicity? trim() uses a regex too, more complicated than /^\s*$/
IMHO, this shows the intent of the code more clear than a regex. Saying foo.trim() == '' is saying "Is this string, ignoring space, empty?". Saying foo.match(/^\s*$/) is asking "Does the string foo match the state machine defined by the regex?". Yes, the regex is simple, but it's still less clear. You're also not guaranteed for trim to be defined via a regex, a lot of JS engines have it built-in.
@yan point taken about native trim. As for the "state machine" part, you're overdoing it :-)
|
5

If one only cares about whitespace at the beginning and end of the string (but not in the middle), then another option is to use String.trim():

"    your string contents  ".trim();

// => "your string contents"

Comments

2

http://jsfiddle.net/DqGB8/1/

This is my solution

var error=0;
var test = [" ", "   "];
 if(test[0].match(/^\s*$/g)) {
     $("#output").html("MATCH!");
     error+=1;
 } else {
     $("#output").html("no_match");
 }

Comments

0

Had similar problem, was looking for white spaces in a string, solution:

  • To search for 1 space:

    var regex = /^.+\s.+$/ ;
    

    example: "user last_name"

  • To search for multiple spaces:

    var regex = /^.+\s.+$/g ;
    

    example: "user last name"

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.