jQuery – Validate the form when the submit button is clicked

Here is a simple jQuery script which could use the validate a mandatory input.

(function ($) {
  $(document).ready(function() {    
    $('#<submit-button-id>').click(function(event){    
      var x = $('<input-id>').val();
      if (x == null || x == '';) {
        alert('Please fill in the input.');
        event.preventDefault();
      }
    })
  });
})(jQuery);

 

Done =)

Reference: StackOverflow – Intercept a click on submit button and check validity of form jQuery

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.