Validate HTML form using ajax request to another page

451
January 30, 2017, at 10:03 PM

I want to use another page of my site to validate HTML form input. If the input is NOT valid, I want to show an error somewhere on my form page without reloading. And otherwise, if it IS valid, I want to submit the form normally.

After reading similar SO questions, I didn't found any exact solution, but actually invented my own, which works for me. Though, I still think that my solution is a bit weird, so I'm asking for proper ideas.

HTML:

 <form id="form" method="POST"> ... some inputs ... </form>
 <button id="submit" type="submit">Save</button>
 <p id="errors"></p>

JS:

$('#form').submit(function (event) {
  var formData = $(this).serializeArray();
  event.preventDefault();
  $.ajax({
         type: "POST",
         url: '/validate/',
         data: formData,
         success: function(data) {
            if(!data)
            {
              $('#form').unbind('submit');
              $('#submit').trigger('click');
            }
            else
            {
                $('#errors').text(data);
            }
        }
      });
return false;
});

NB: It works as expected, I'm asking for better solutions.

Rent Charter Buses Company
READ ALSO
Find all `*.html` but not `*.tmp.html` using JavaScript regex

Find all `*.html` but not `*.tmp.html` using JavaScript regex

I have a large project that has many *html files and many *

447
REST API -MS365 share point

REST API -MS365 share point

I would like to create site and list in MS365 sharepointWhen I am referring documents related to MS sharepoint365,couldn't get much info

392
not working response.end() in nodejs

not working response.end() in nodejs

According to knowledge of mine,the responseend() should be called in after every response according to the nodejs api documenetation it which has described here

514
Angular 2 *ngFor and form submit ceased working when moved to observables

Angular 2 *ngFor and form submit ceased working when moved to observables

I had an app initiating course components like this:

375