Skip to content

Instantly share code, notes, and snippets.

@jasonsemko
Created February 20, 2014 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsemko/9125266 to your computer and use it in GitHub Desktop.
Save jasonsemko/9125266 to your computer and use it in GitHub Desktop.
FX Javascript Validation
/**
* This file contains the logic for all of the basic form validation across the site.
* For validation we are using the
*
* jQuery Validation Plugin
*
* http://jqueryvalidation.org/
*
* @author Jason Semko
*/
var FX = FX || {},
FORM_ALTERED = false;
FX.Validation = FX.Validation || (function () {
/**
* Validation variables
*/
var rules = {};
/**
* First Name
* Last Name
*/
rules['fname'] = rules['lname'] = {
required: true,
minlength: 2,
digits: false
};
/**
* Email
* Confirm Email
*/
rules['email'] = {
required: true,
email: true
};
rules['confirm-email'] = $.extend(true, {'equalTo':'#email'}, rules['email']);
/**
* Zip Code
*/
rules['zip'] = {
required: true,
minlength: 5,
maxlength: 5,
digits: true
};
/**
* Password
* New Password
* Old Password
* Confirm Password
*/
rules['password'] = rules['new-password'] = rules['old-password'] = rules['confirm-password'] = {
required: true,
minlength: 6,
maxlength: 20
};
rules['confirm-password'] = $.extend(true, {'equalTo':'#new-password'}, rules['password']);
/**
* Pin
* New PIN
* Old PIN
* Confirm PIN
*/
rules['pin'] = rules['new-pin'] = rules['old-pin'] = {
required: true,
minlength: 4,
maxlength: 4,
digits: true
};
rules['confirm-pin'] = $.extend(true, {'equalTo':'#new-pin'}, rules['pin']);
/**
* Phone Number
*/
rules['phone'] = {
phoneUS: true
};
/**
* Username
*/
rules['username'] = {
required: true,
minlength: 2,
maxlength: 20,
};
/**
* Terms (Checkbox)
* Cable Provider (Dropdown)
*/
rules['terms'] = rules['cable-provider-req'] = {
required: true
};
/**
* Logic to handle all validations.
* This utilizes the 'rules' object above into the validation.
* If a field does not exist it is ignored.
*
* @author Jason Semko
*/
function validationHandler(v) {
v.each(function(index, el) {
$('#' + $(this).attr('id')).validate({
rules: rules,
ignore: [],
errorPlacement: function(error, element) {
handleErrors(error, element);
}
});
});
}
/**
* Private function which handles the placements of errors.
*
* PIN Numbers require the error label outside of the .input-pin div
* Regular text inputs require an immediate appension
* Dropdowns require an error outside of the 'chosen' class
*
* @param HTML Element error The error
* @param jQuery Object element The Jquery DOM element
*
* @author Jason Semko
*/
function handleErrors(error, element) {
var type = 'text';
//What type of element are we dealing with? Assume text as default.
if(element.closest('.input-pin').length) { type = 'pin'; }
if(element.prop('tagName').toLowerCase() === 'select'){ type = 'select'; }
if(element.closest('.checkbox').length) { type = 'checkbox'; }
switch(type) {
case 'pin':
error.insertAfter(element.closest('.input-pin'));
break;
case 'select':
if(element.next('.chosen-container').length) {
error.insertAfter(element.next('.chosen-container'));
} else {
error.insertAfter(element);
}
break;
case 'checkbox':
error.insertAfter(element.closest('.checkbox'));
break;
default:
error.insertAfter(element);
break;
}
}
return {
init: function () {
var validation = $('.validation');
if(validation.length) {
validationHandler(validation);
}
}
}
})();
$(document).ready(function () {
FX.Validation.init();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment