简体   繁体   English

验证表单在提交之前输入ajax

[英]validation form input ajax before submit

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. 我试图制作数据输入表单,但是当在按Save时仍可以保存一个空的输入表单时,以及如何在输入为空时将不保存它。

this is my form code: https://jsfiddle.net/bubyj5xj/ 这是我的表单代码: 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();
    }
  });
});
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();
   }
}
form_text_arr = {'text': jQuery("form[name='put_ad_form']").serializeArray()};


after you need check each value: 在您需要检查每个值之后:

 var reg=new RegExp(/^[A-Za-z0-9+*-]{1,10}$/);
if(reg.test(text.val)){
    .......
}

or 要么

if(text.val == ''){
  alert('please input text');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM