Solved

Best way to validate from fields before ajax submission

Posted on 2014-03-22
8
502 Views
Last Modified: 2014-03-25
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
Comment
Question by:Rocking
8 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 39948281
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39948293
0
 

Author Comment

by:Rocking
ID: 39948799
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
Independent Software Vendors: 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!

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 80 total points
ID: 39949168
I prefer this

$(function() {
  $("#formId").on("submit",function(e) {
    e.preventDefault();
    if (.......) {
      $.ajax(...)
    }
  });
});
0
 

Author Comment

by:Rocking
ID: 39950827
What if we call the validate function in   beforeSend : function() {} ?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39951711
Why even start the ajaxing if not valid?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39951716
Why even start the ajaxing if not valid?
0
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 30 total points
ID: 39952304
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Make check boxes work 8 41
Accordion won't close correctly 5 12
DataTable column sorting incorrectly 2 17
modify change color of text 9 33
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question