saiful saiful - 1 year ago 56
Javascript Question

validation form input ajax before submit [SOLVED]

I have tried to make the data input form but when an empty input form can still be saved when in press save and how when the input is empty then it will not be save.

this is my form code: https://jsfiddle.net/bubyj5xj/

$(".btn_save_news_item").button().click(function() {
var form = $("#form_news_item");
var json = ConvertFormToJSON(form);
$.ajax({
type: "POST",
contentType: "application/json",
url: BASE_URL + "setting/news",
data: JSON.stringify(json),
success: function(response) {
console.log(response);
resetForm();
reloadGrid();
}
});
});

Answer Source
var form = $("#form_news_item");  
if(form=="")
{
    alert("Please fill field");
}
else
{
   $.ajax({
   type: "POST",
   contentType: "application/json",
   url: BASE_URL + "setting/news",
   data: JSON.stringify(json),
   success: function(response) {
   console.log(response);
   resetForm();
   reloadGrid();
   }
}