일부 입력이있는 웹 양식이 있습니다.이 필드의 데이터를 보내는 Ajax가 있습니다. 빈 필드를 확인하기 위해 유효성 검사를 수행합니다. 그러나 코드는 그렇지 않습니다. 작업. 나는 폼을 submiting the default valus로 필드를 쉬고 있다고 생각한다. 검증을 수행하기 전에 submit.please 도움을 받기 전에 유효성 체크를 먼저 할 수있는 방법이 있는가?JQuery : JQuery 유효성 검사 전에 양식을 제출하는 이유
function validate_fields() {
var empty_inputs = $('.mandatory').filter(function() {
return !$(this).val();
}).length;
//Checking if any Empty input or select that has class mandatory
if (empty_inputs > 0) {
$('form').find('input.mandatory, select.mandatory').each(function() {
if ($(this).val() == '' || $(this).val() == null) { //Change the color if empty input or select
$(this).css('background-color', '#FFCECE');
} else {
$(this).css('background-color', 'transparent');
};
});
return false;
} else {
return true;
}
}
$('#button_save').click(function() {
$("form").on("submit", function(event) {
var status;
status = validate_fields();
// if fuction validate_fields return false then Stop submit
if (status == false) {
return false;
}
var dt = $(this).serializeArray();
event.preventDefault();
//This Ajax Post the Input data if validation return true.
$.ajax({
url: 'send_data.php',
type: "post",
async: true,
data: dt,
dataType: 'html',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function(data) {
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
TRY)과 같은 값 길이의 유효성을 검사하여 코드는 괜찮아 보인다. – GauravKP