if input has required alert an error if emptyin jquery
You can use the jQuery val() and trim() functions to check if the input field is empty or not. Here's an example:
HTML:
<input type="text" id="myInput" required>
<button id="myButton">Submit</button>
<div id="error"></div>
jQuery:
$(document).ready(function() {
$('#myButton').click(function() {
var inputVal = $('#myInput').val().trim();
if (inputVal === '') {
$('#error').text('Input field cannot be empty!');
return false;
} else {
$('#error').empty();
return true;
}
});
});
In this example, the code checks if the input field is empty when the submit button is clicked. If it's empty, it displays an error message in the #error div. If it's not empty, it clears the error message and allows the form to be submitted
原文地址: https://www.cveoy.top/t/topic/f2qo 著作权归作者所有。请勿转载和采集!