Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function(){
  2.     //Hide the error/success message response on load
  3.     $('#response').hide();
  4.     $('form input').on("blur", function(){
  5.         //Hide the reponse from last time
  6.         $('#response').hide();
  7.         var formInputs = new Array();
  8.         //Get the form ID
  9.         var id = $(this).closest('form').attr('id');
  10.         $('#' + id + ' input').each(function(){
  11.             //Remove any previously set errors
  12.             $(this).removeClass('error');
  13.             //Create a temp object that holds the input data
  14.             var obj = {
  15.                 'value': $(this).val(),
  16.                 'id': $(this).attr('id')
  17.             };
  18.             //Add the object to the array
  19.             formInputs.push(obj);
  20.         });
  21.         $.ajax({
  22.             url: 'validate.php',
  23.             type: 'POST',
  24.             data: {
  25.                 'inputs': formInputs
  26.             },
  27.             success: function(data) {
  28.                 //Check to see if the validation was successful
  29.                 if (data.success) {
  30.                     //Turn the response alert into a success alert
  31.                     $('#response').addClass('alert-success');
  32.                     $('#response').removeClass('alert-error');
  33.                     //Add the success message text into the alert
  34.                     $('#response').html("<i class='icon-ok'></i> Form validated successfully!").fadeIn();
  35.                     $("#" + id).submit();
  36.                 } else {
  37.                     //There were some errors
  38.                     //Turn the response alert into an error alert
  39.                     $('#response').removeClass('alert-success');
  40.                     $('#response').addClass('alert-error');
  41.                     //Create a message and a HTML list to display in the response alert
  42.                     var list = "<p class='form-group'><i class='icon-remove-sign form-group'></i> Folgende Fehler sind aufgetreten: </p><ul>";
  43.                     //Loop through each error message and add it to the list
  44.                     for(var i in data.errors) {
  45.                         $('#' + data.errors[i].field).addClass("error");
  46.                         list += "<li>" + data.errors[i].msg + "</li>";
  47.                     }
  48.                     //Add the HTML to the response alert and fade it in
  49.                     $('#response').html(list).fadeIn();
  50.                 }
  51.             }
  52.         });
  53.     });
  54. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement