[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 517
  • Last Modified:

Best way to validate from fields before ajax submission

Hi,

I need to validate form and once the form is validated then the ajax should submit the form?

validate method -> true -> call ajax -> form submitted...
0
Rocking
Asked:
Rocking
2 Solutions
 
MrunalCommented:
0
 
Michel PlungjanIT ExpertCommented:
0
 
RockingAuthor Commented:
is it the best approach to create a plain java script method to do the validation and then check in the another function its return value and then call ajax to submit the form?

function validate(){
xxx
xxx
xxxx

return true/false
}

function SubmitFrm() {
 var chkfrmValidation = validate();
//if the return value true go for ajax
       {
            $.ajax({
                  type : 'post',
                  url : 'testsubmit',
                  data : $('#registerform').serialize(),
                  async : false,
                  dataType : "text",
                  beforeSend : function() {
                        $.blockUI({ message: '<h1><img src="../img/loading.gif" /> Processing...</h1>'});
                  },
                  complete : function() {
                        $.unblockUI();
                  },
                  success : function(data) {
                  $.unblockUI();
                        alert(data);
                        
                  },
                  error : function(data) {
                        // check status && error
                  }

            });
      }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Michel PlungjanIT ExpertCommented:
I prefer this

$(function() {
  $("#formId").on("submit",function(e) {
    e.preventDefault();
    if (.......) {
      $.ajax(...)
    }
  });
});
0
 
RockingAuthor Commented:
What if we call the validate function in   beforeSend : function() {} ?
0
 
Michel PlungjanIT ExpertCommented:
Why even start the ajaxing if not valid?
0
 
Michel PlungjanIT ExpertCommented:
Why even start the ajaxing if not valid?
0
 
Slick812Commented:
greetings him121, Here you ask -
 "is it the best approach to create a plain java script method to do the validation and then check in the another function its return value and then call ajax to submit the form?"

Right, , That would get it done, to use the function validate(), and if TRUE then proceed to the ajax.

The  ajax   beforeSend : function()   is NOT the place to do the validate, as this beforeSend is suppose to be a place to RE-configure the ajax headers and other PRE ajax send adjustments or Call in a wait image, , , , beforeSend is not a place to do work outside of the necessary Ajax stuff .

No one can say if "is it the best approach", since there are many ways to go through the processes (validation, get form input data, send the ajax to server).

But you seem to be new at doing this sort of thing, so are you asking for suggestions on how to do this with plain javascript, or with just jQuery javascript,

or what sort of answers could help you here?
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now