Skip to content

Instantly share code, notes, and snippets.

@patrickml
Forked from anonymous/Pretty URL
Last active October 1, 2015 23:02
Show Gist options
  • Save patrickml/1190cf52bd9c51c832f9 to your computer and use it in GitHub Desktop.
Save patrickml/1190cf52bd9c51c832f9 to your computer and use it in GitHub Desktop.
$('#quoteForm').click(function(){
// Variables with data
var inputs = $('.modal-body').find('.form-group').children().not('label');
// Open variables
var data = {};
var rules = {};
var trueAll = [];
// Functions with in scope
function isTrue(element,index,array){
return element === true;
}
// Organize Data
$.each(inputs,function(key,values) {
data[key] = {id: $elm.attr('id'), rule: $(values).attr('data-rule'), value: $elm.val()};
});
$.each(data,function(key,values){
switch (values.rule) {
case "date":
rules[key] = {pass: isNaN(Date.parse(values.value)), id: values.id};
break;
case "phone":
rules[key] = {pass: isNaN(values.value), id: values.id};
break;
case "string":
case "email":
rules[key] = {pass: values.value.length > 0, id: values.id};
break;
default:
}
});
// Animations
$.each(rules,function(key,values){
$('#'+values.id).closest('.form-group').toggleClass('has-error', !values.pass);
});
// Backend integration
if(trueAll.every(isTrue)){
$.ajax({
url: '/quote-form',
type: 'POST',
dataType:'json',
data: data,
success: function (data) {
if(data.success){
inputs.val('');
$('#myModal').modal('hide');
}
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment