jQuery Validation: How to Alert for Empty Required Inputs
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.
原文地址: https://www.cveoy.top/t/topic/osKu 著作权归作者所有。请勿转载和采集!