-
-
Save patrickml/1190cf52bd9c51c832f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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