Solved

Best way to validate from fields before ajax submission

Posted on 2014-03-22
8
495 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
 
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
DevOps Toolchain Recommendations

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

 

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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript waiting 14 69
jquery easyautocomplete for mutiple text boxes 9 55
Angular - "nest" service calls? 18 24
what is the difference between call and apply 1 26
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now