Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * quadmail.validation.js
- * Quadmail Form Validation plugin
- */
- QM(function() {
- // Loop through each validation form on the page
- QM('form.validation').each(function() {
- var $element = QM(this), element = this;
- quadmail.debug('validation', 'Found a validation form.');
- // Form submit event.
- $element.submit(function() {
- quadmail.debug('validation', 'Validating Form');
- // Hide the errors container.
- $element.children('td.validation').hide();
- var v = "";
- var values = new Array();
- // Loop through each input to check for validation, and append to the validation data string.
- $element.children('input').each(function() {
- var $input = QM(this);
- var data = $input.attr('data-validation');
- var id = $input.attr('name');
- if (data != null)
- v += name + ":" + data + "|";
- // Append the information to the values array.
- values[id] = $input.val();
- // Remove the error class.
- $input.removeClass('error');
- });
- values['validation'] = v;
- quadmail.debug('validation', 'Validation string: ' + v);
- // Submit the form to the server.
- // TODO: implement loading show here.
- $.post($element.attr('data-action'), values, function(data) {
- // TODO: implement loading hide here.
- if (data.errors != null) {
- // Errors found. Loop through them and add them to the errors list.
- $errorsContainer = $element.children('td.validation');
- if ($errorsContainer != null) {
- $errorsContainer.children('ul').html('');
- for (var i = 0; i < data.errors.length; i++) {
- $errorsContainer.children('ul').append('<li>' + data.errors[i].message + '</li>');
- $element.children('input[name="' + data.errors[i].field + '"]').addClass('error');
- }
- $errorsContainer.show();
- }
- }
- $element.trigger('validated');
- }, 'json');
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement