Check if string is empty with jquery

Check if string is empty with jquery

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why !$textarea.val() Fails for Empty Textareas

The core issue in your validation logic stems from a common misunderstanding of how jQuery handles the value of a <textarea> element when it is completely empty. In your code:

var comment = $("#comments").val();
if ((!comment) || (comment > 100)) { ... }

You are checking if !comment evaluates to true. While this works perfectly for standard input fields like <input type="text">, it fails for textareas because of how the .val() method behaves. When a user leaves a textarea blank, jQuery's .val() does not return an empty string (""). Instead, it returns the string "undefined".

In JavaScript, both an empty string and the string "undefined" are falsy values, so !comment technically evaluates to true. However, the real problem lies in the second part of your condition: (comment > 100). This comparison attempts to perform a mathematical operation on a string.

When you compare the string "undefined" against the number 100 using the greater-than operator (>), JavaScript coerces both operands into numbers. The string "undefined" becomes the numeric value NaN (Not-a-Number). In JavaScript, any comparison involving NaN returns false. Consequently, your condition logic breaks down because you are relying on a mathematical check that invalidates the entire expression structure when the input is empty.

The Correct Way to Check for Empty Textareas

To reliably detect an empty textarea, you must explicitly check against an empty string rather than relying on truthiness or numerical comparisons. This approach ensures consistency across all input types and avoids type coercion pitfalls.

Update your validation block as follows:

var comment = $("#comments").val();
if (comment === "" || comment.length > 100) {
    $("label#commentErr").show();
    $("#comments").focus();
    alert("Comment cannot be empty or exceed 100 characters.");
    return false;
}

By using === "", you are strictly checking if the value is an empty string. If the textarea is left blank, .val() returns "", making the first part of the OR condition true and triggering the validation error immediately. The second part, comment.length > 100, now operates on a valid integer only when the input actually contains characters.

Handling Whitespace

A frequent edge case in form validation is handling whitespace. A user might click into a textarea, press the spacebar once, and hit submit. jQuery's .val() method includes this single space in the returned string. If your requirement is to treat whitespace-only inputs as empty, you need to trim the value before checking:

var comment = $("#comments").val().trim();
if (comment === "" || comment.length > 100) { ... }

This ensures that (spaces) are treated the same as an empty input, preventing accidental submission of blank lines.

Moving Beyond jQuery for Validation

While your current script uses jQuery for DOM manipulation and AJAX handling, which is perfectly valid in many legacy codebases, modern Laravel projects often leverage server-side validation to reduce client-side logic complexity. If you are building a new feature or refactoring an existing application on the LaravelCompany.com platform, consider implementing strict validation rules directly in your Rules class or controller methods.

Server-side validation provides a robust safety net. Even if the JavaScript check for the textarea fails due to a browser quirk or a future version update of jQuery, the server will still reject the request if the comment field is missing or too long. This decouples the user experience from the application's core data integrity logic.

In summary, always assume that .val() on a textarea returns an empty string when blank, not "undefined". Stick to explicit string equality checks (=== "") and length properties for all validation logic to ensure predictable behavior across different input elements.

Our network