Created
October 1, 2015 22:36
-
-
Save anonymous/0799d42556122d8c7ad0 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){ | |
var ids = $(values).attr('id'); | |
var rules = $(values).data('rule'); | |
var va = $('#'+ids).val(); | |
data[key] = {id:ids,rule:rules,value:va}; | |
}); | |
$.each(data,function(key,values){ | |
switch (values.rule) { | |
case "date": | |
if(isNaN(Date.parse(values.value))){ | |
rules[key] = {pass:false,id:values.id}; | |
}else{ | |
rules[key] = {pass:true,id:values.id}; | |
} | |
break; | |
case "phone": | |
if(isNaN(values.value)){ | |
rules[key] = {pass:false,id:values.id}; | |
}else{ | |
rules[key] = {pass:true,id:values.id}; | |
} | |
break; | |
case "string": | |
case "email": | |
if(values.value.length > 0){ | |
rules[key] = {pass:true,id:values.id}; | |
}else{ | |
rules[key] = {pass:false,id:values.id}; | |
} | |
break; | |
default: | |
} | |
}); | |
// Animations | |
$.each(rules,function(key,values){ | |
if(values.pass === false){ | |
$('#'+values.id).closest('.form-group').addClass('has-error'); | |
trueAll.push(values.pass); | |
}else{ | |
$('#'+values.id).closest('.form-group').removeClass('has-error'); | |
trueAll.push(values.pass); | |
} | |
}); | |
// Backend integration | |
if(trueAll.every(isTrue)){ | |
$.ajax({ | |
url: '/quote-form', | |
type: 'POST', | |
dataType:'json', | |
data: data, | |
success: function (data) { | |
if(data.success){ | |
$.each(rules,function(key,values){ | |
$('#'+values.id).val(''); | |
}); | |
$('#myModal').modal('hide'); | |
} | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment