Jürgen Paul
Jürgen Paul

Reputation: 15037

trim and check if empty input

if(!$.trim($('.xlarge').value).length) {
    return false;
}

And the HTML is:

<input class="xlarge" name="name">

I checked on my console if the syntax is right, it is right, but even if the value is not empty it still doesn't submit the form. What's wrong?

Upvotes: 7

Views: 14358

Answers (3)

Manse
Manse

Reputation: 38147

No need to check length just use :

if(!$.trim($('.xlarge').val())) {
     return false;
}

val() returns the value of the input - docs for val()

Discussed here Check if inputs are empty using jQuery

Upvotes: 7

stamp
stamp

Reputation: 120

if($.trim($('.xlarge').val()) == "") {
    return false;
}

Upvotes: 3

jabclab
jabclab

Reputation: 15062

Instead of:

$('.xlarge').value

You should use:

$('.xlarge').val()

.val() is the jQuery function which will return the value of the first element in the set of matched elements. Here's a link to the documentation: http://api.jquery.com/val/.

Upvotes: 2

Related Questions