Skip to content

Instantly share code, notes, and snippets.

@JimboFromLimbo
Created April 17, 2016 23:42
Show Gist options
  • Save JimboFromLimbo/7c291fb17a482de1eb1e8cea48a04085 to your computer and use it in GitHub Desktop.
Save JimboFromLimbo/7c291fb17a482de1eb1e8cea48a04085 to your computer and use it in GitHub Desktop.
function checkSection(element, nextElement) {
if ($(element).data("required").filled == $(element).data("required").total) {
$(nextElement).slideDown('slow');
} else {
$(nextElement).slideUp('slow');
}
}
function checkField(element, parent)
{
// alert('radio button');
if ($(element).attr("type") == "radio") {
if ($("input[name=" + $(element).attr("name") + "]:checked").length > 0) {
if (!$(element).parent().hasClass("filled")) {
if ($(parent).data("required").filled < ($(parent).data("required").total)) {
$(parent).data("required").filled++;
$(element).parent().addClass("filled");
}
}
console.log($(parent).data("required").filled);
$(element).parent().removeClass("failed-radio");
}
}
else
{
if ($(element).val().length == 0) {
if ($(element).hasClass("filled")) {
$(parent).data("required").filled--;
$(element).removeClass("filled");
}
$(element).addClass("failed");
console.log($(parent).data("required").filled);
}
else {
if (!$(element).hasClass("filled")) {
if (($(parent).data("required").filled < $(parent).data("required").total)) {
$(parent).data("required").filled++;
$(element).addClass("filled");
}
}
console.log($(parent).data("required").filled);
$(element).removeClass("failed");
}
}
// // if the element has no value, we
// if ($(element).val().length == 0)
}
function addRequiredState(element)
{
// console.log ('class should be added')
console.log(element);
$(element).addClass('required');
}
function checkForm(fadeoutelement , fadeinelement) {
var errors = 0;
$('.required').each(function(i, e)
{
if ($(e).attr("type") == "radio") {
// you have to check if any of the radio buttons with the same name are checked
if ($("input[name=" + $(e).attr("name") + "]:checked").length == 0)
{
if ($(e).is(":visible")) {
console.log('it sees that there is a button');
$(e).parent().addClass("failed-radio");
errors++;
}
}
}
else if ($(this).val().length == 0)
{
if ($(this).is(":visible"))
{
$(this).addClass("failed");
errors++;
}
}
})
if (errors == 0) {
$(fadeoutelement).fadeOut(500);
$(fadeinelement).delay(500).fadeIn(1500);
} else {
console.log(errors);
}
};
$('#placeofbirth-1').data("required", {total: $('#placeofbirth-1 .required').length, filled: 0});
$('#placeofbirth-1 .required').on('keyup click', function() { checkField($(this), $('#placeofbirth-1')); });
$('#placeofbirth-1').on('keyup click', function() { checkSection($('#placeofbirth-1'), $('.dropdown-id'));});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment