When building interactive web forms, validating user input is crucial for a smooth user experience. One of the most common validation tasks is checking whether input fields are empty. jQuery provides several powerful methods to accomplish this task efficiently.
In this comprehensive guide, we’ll explore various techniques to check if inputs are empty using jQuery.
Understand jQuery Input Validation
jQuery’s .val() method is the primary tool for checking input values. This method retrieves the current value of form elements and can be used to determine if an input field contains data.
Method 1: Basic Empty Input Check Using .val()
The simplest way to check if an input is empty is using the .val() method combined with length or direct comparison:
// Check if input is empty
if ($('#myInput').val() === '') {
console.log('Input is empty');
}
// Alternative using length
if ($('#myInput').val().length === 0) {
console.log('Input is empty');
}
// Using jQuery's trim() to handle whitespace
if ($.trim($('#myInput').val()) === '') {
console.log('Input is empty or contains only whitespace');
}You can refer to the screenshot below to see the output.

Method 2: Use the :empty Selector
jQuery’s :empty pseudo-class represents elements that have no children at all. However, this method works differently for input fields:
// Note: :empty doesn't work directly with input values
// It checks if the element has no child nodes
if ($('#myDiv:empty').length > 0) {
console.log('Element has no content');
}You can refer to the screenshot below to see the output.

The empty selector doesn’t check input values but rather whether elements contain child nodes.
Method 3: Real-Time Validation with Event Handlers
For dynamic form validation, combine empty checks with event handlers:
$(document).ready(function() {
// Check on input change
$('#myInput').on('input', function() {
if ($(this).val() === '') {
$(this).addClass('error');
$('#errorMessage').text('This field is required');
} else {
$(this).removeClass('error');
$('#errorMessage').text('');
}
});
// Check on form submission
$('#myForm').on('submit', function(e) {
var isEmpty = $('#myInput').val() === '';
if (isEmpty) {
e.preventDefault();
alert('Please fill in all required fields');
}
});
});You can refer to the screenshot below to see the output.

Method 4: Check Multiple Inputs Simultaneously
When dealing with multiple form fields, you can check all inputs at once:
function checkAllInputs() {
var allFilled = true;
$('input[type="text"], input[type="email"], textarea').each(function() {
if ($.trim($(this).val()) === '') {
$(this).addClass('error');
allFilled = false;
} else {
$(this).removeClass('error');
}
});
return allFilled;
}
// Usage
$('#submitBtn').click(function() {
if (checkAllInputs()) {
console.log('All fields are filled');
} else {
console.log('Some fields are empty');
}
});You can refer to the screenshot below to see the output.

Method 5: Advanced Validation with Custom Functions
Create reusable functions for different validation scenarios:
// Generic empty check function
function isEmpty(selector) {
return $.trim($(selector).val()) === '';
}
// Check specific input types
function validateForm() {
var validationRules = {
'#firstName': 'First name is required',
'#email': 'Email address is required',
'#phone': 'Phone number is required'
};
var errors = [];
$.each(validationRules, function(selector, message) {
if (isEmpty(selector)) {
errors.push(message);
$(selector).addClass('error');
} else {
$(selector).removeClass('error');
}
});
return errors;
}Visual Feedback Implementation
Provide users with immediate visual feedback when inputs are empty:
$(document).ready(function() {
$('.required-field').on('blur', function() {
var $this = $(this);
var fieldValue = $.trim($this.val());
if (fieldValue === '') {
$this.css('border', '2px solid red');
$this.next('.error-message').remove();
$this.after('<span class="error-message" style="color: red;">This field is required</span>');
} else {
$this.css('border', '2px solid green');
$this.next('.error-message').remove();
}
});
});Best Practices for jQuery Input Validation
- Always trim whitespace: Use $.trim() to handle spaces and tabs
- Provide immediate feedback: Validate on blur or input events
- Use consistent styling: Apply CSS classes for error states
- Handle different input types: Text, email, number, textarea, etc.
- Prevent form submission: Stop invalid forms from being submitted
Complete Working Example
Here’s a comprehensive example combining all techniques:
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.error { border: 2px solid red; }
.success { border: 2px solid green; }
.error-message { color: red; font-size: 12px; }
</style>
</head>
<body>
<form id="contactForm">
<input type="text" id="name" placeholder="Your Name" class="required">
<input type="email" id="email" placeholder="Your Email" class="required">
<textarea id="message" placeholder="Your Message" class="required"></textarea>
<button type="submit">Submit</button>
</form>
<script>
$(document).ready(function() {
// Real-time validation
$('.required').on('input blur', function() {
validateField($(this));
});
// Form submission validation
$('#contactForm').on('submit', function(e) {
e.preventDefault();
if (validateForm()) {
alert('Form is valid!');
} else {
alert('Please fill all required fields');
}
});
function validateField($field) {
if ($.trim($field.val()) === '') {
$field.removeClass('success').addClass('error');
return false;
} else {
$field.removeClass('error').addClass('success');
return true;
}
}
function validateForm() {
var isValid = true;
$('.required').each(function() {
if (!validateField($(this))) {
isValid = false;
}
});
return isValid;
}
});
</script>
</body>
</html>Checking if inputs are empty using jQuery is a fundamental skill for web developers. The .val() method, combined with proper event handling, provides robust validation capabilities. Remember to always trim whitespace, provide visual feedback, and validate both on user interaction and form submission for the best user experience.
By implementing these techniques, you’ll create more user-friendly forms that guide users toward successful completion while preventing invalid data submission.
You may also read:
- Handle Dropdown Change Event in jQuery
- Execute Functions After Page Load Using jQuery
- Check Which Radio Button is Selected Using jQuery
- 51 jQuery Examples with Source Code

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.