Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multipart jQuery validation

I'm trying to validate different parts of a form separately. Unfortunately, the form is generated by a CMS, so I'm limited in my manipulation.

I've tried creating an array of validate objects, using the current form section as an index. Ie:

  //initialize validation
  validators = [
    $('#donation_amount').validate({ rules:{ amount: { required: true } } }),
    $('#personal_information').validate({ rules:{ Street: { required: true } } })
   ];

and shifting through the sections like so:

  $('#btn-next').click(function() {
    //if validation is true, show next page
    if (validators[curOrder].valid()) {
      var old = $('.active');
      var oldOrder = old.attr('data-order');
      var newOrder = parseInt(oldOrder) + 1;
      old.removeClass('active');
      $("[data-order='" + newOrder + "']").addClass('active');
      curOrder = newOrder;
    }else{
      console.log("invalid");
   }
});

The validation, however, is always returning true.

Here's the page in question: https://salsa3.salsalabs.com/o/50388/p/salsa/donation/common/public/?donate_page_KEY=8461

like image 653
Asherlc Avatar asked Sep 25 '12 20:09

Asherlc


1 Answers

Why are you even using .validate plugin when you are writing a little javascript yourself. On click, just check the value of the inputs (like $('#myInput').val().trim() == "") and show/hide the respective error div against each input.

Further, for multipart validation, validate only the required fields and continue what is supposed to.

$('#btn-next').click(function() {
var amountValid = $('#donation_amount').val().trim() == '' ? false : true;
var infoValid = $('#personal_information').val().trim() == '' ? false : true;

if (amountValid && infoValid) {
  var old = $('.active');
  var oldOrder = old.attr('data-order');
  var newOrder = parseInt(oldOrder) + 1;
  old.removeClass('active');
  $("[data-order='" + newOrder + "']").addClass('active');
      curOrder = newOrder;
    }else{
      console.log("invalid");
    }
});
like image 92
Taha Rehman Siddiqui Avatar answered Oct 16 '22 07:10

Taha Rehman Siddiqui