Assuming the required input has a class of "required", you can use the following code to check if it is empty and display an error message:

$('form').submit(function(event) {
  $('.required').each(function() {
    if ($(this).val() === '') {
      event.preventDefault();
      alert('Please fill in all required fields');
    }
  });
});

This code listens for the form submit event, then loops through each input with the "required" class. If the input is empty, it prevents the form from submitting and displays an alert message

if input has required tag alert an error if emptyin jquery

原文地址: https://www.cveoy.top/t/topic/f2qq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录