agyanadda@gmail.com

jQuery Form Handling

jQuery Forms 1 min read Updated Dec 1, 2026 Topic 17 of 25

Submit event

$('#signup').on('submit', function (e) {
  e.preventDefault(); // stop page reload
  // check and send
});

Read values

  • Text: $.trim($('#name').val())
  • Select: $('#city').val()
  • Checkbox: $('#agree').is(':checked')
  • Radio: $('input[name=gender]:checked').val()

Show errors

Add an error class and message next to the field; remove them when the value is fixed.

serialize()

Turn the whole form into a query string, ready for AJAX:

$('#signup').serialize(); // "name=Asha&city=Patna&agree=on"

.serializeArray() gives an array of { name, value } objects.

Fields need a name attribute to be included. Always validate again on the server.

Example

Example — Validated sign-up form HTML
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<style>
  input, select { display:block; margin-top:6px; padding:6px; width:240px; }
  .error { border:2px solid crimson; }
  .msg { color:crimson; font-size:13px; }
</style>

<form id="signup" novalidate>
  <input name="name" id="name" placeholder="Full name"><span class="msg"></span>
  <input name="mobile" id="mobile" placeholder="Mobile"><span class="msg"></span>
  <select name="city" id="city"><option value="">Select city</option><option>Gaya</option><option>Patna</option></select><span class="msg"></span>
  <label style="display:block;margin-top:8px"><input type="checkbox" name="agree" id="agree" style="display:inline;width:auto"> I agree to the terms</label><span class="msg"></span>
  <button style="margin-top:10px">Sign up</button>
</form>
<pre id="out"></pre>

<script>
  $(function () {
    function check($field, ok, message) {
      $field.toggleClass('error', !ok);
      $field.nextAll('.msg').first().text(ok ? '' : message);
      return ok;
    }
    $('#signup').on('submit', function (e) {
      e.preventDefault();
      let valid = true;
      valid = check($('#name'), $.trim($('#name').val()).length >= 3, 'Enter at least 3 letters') && valid;
      valid = check($('#mobile'), /^[6-9]\d{9}$/.test($('#mobile').val()), 'Enter a 10-digit mobile number') && valid;
      valid = check($('#city'), $('#city').val() !== '', 'Please choose a city') && valid;
      valid = check($('#agree').parent(), $('#agree').is(':checked'), 'Please accept the terms') && valid;
      $('#out').text(valid ? 'Valid! Data: ' + $(this).serialize() : '');
    });
  });
</script>

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which method turns a form into a query string?

Exercise 2 True / False

Fields without a name attribute are included in serialize().

Exercise 3 Fill in the Blank

$('#agree').is(':___') checks if a checkbox is ticked.

Frequently Asked Questions

Yes, the popular jQuery Validation Plugin handles rules and messages. For small forms, your own checks like this example are enough.