Link to home
Start Free TrialLog in
Avatar of Rocking
Rocking

asked on

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...
Avatar of Mrunal
Mrunal
Flag of India image

Avatar of Rocking
Rocking

ASKER

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
                  }

            });
      }
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rocking

ASKER

What if we call the validate function in   beforeSend : function() {} ?
Why even start the ajaxing if not valid?
Why even start the ajaxing if not valid?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial