7
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?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Jürgen Paul
  • 14,299
  • 26
  • 93
  • 133
  • `$('.xlarge').value` will be `undefined` as `$('.xlarge')` is a jQuery object and does not have a `value` property. You either want to access the DOM element `$('.xlarge')[0].value` or use the `.val()` function: `$('.xlarge').val()`. – Felix Kling Jan 06 '12 at 09:57

3 Answers3

7

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

Community
  • 1
  • 1
Manse
  • 37,765
  • 10
  • 83
  • 108
3
if($.trim($('.xlarge').val()) == "") {
    return false;
}
stamp
  • 120
  • 9
2

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/.

jabclab
  • 14,786
  • 5
  • 54
  • 51