Skip to content

Instantly share code, notes, and snippets.

@sebas932
Last active January 23, 2018 14:46
Show Gist options
  • Save sebas932/7b1cba272db2c51d88241951771eafc8 to your computer and use it in GitHub Desktop.
Save sebas932/7b1cba272db2c51d88241951771eafc8 to your computer and use it in GitHub Desktop.
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script type="text/javascript" src="validation_form1.js" ></script>
var $form1, formArray1, formNames1;
$(document).ready(init);
function init() {
// Form 1 - Radios Validation
$form1 = $('input[name="p1"], input[name="p2"], input[name="p3"], input[name="p4"], input[name="p5"], input[name="p6"], input[name="p7"], input[name="p8"], input[name="p9"], input[name="p10"]');
formValues1 = [];
formNames1 = [];
// Event when form 1 radiobuttons is clicked
$form1.on('click', function(e){
// Set parameters
var isChecked = $(this).is(':checked');
var selectedValue = parseInt(this.value);
var selectedName = this.name;
var selectedOptions = formValues1.length;
var isValueRepeated = ($.inArray(selectedValue, formValues1) != -1);
var isNameRepeated = ($.inArray(selectedValue, formValues1) != -1);
// Validate at least 3 options selected and no repeated
if((selectedOptions < 3) && !isValueRepeated && !isNameRepeated){
// Add value TO formValues1
if(!isValueRepeated){
formValues1.push(selectedValue);
}
if(!isNameRepeated){
formNames1.push(selectedName);
}
}else{
// Do nothing
e.preventDefault();
}
console.log(formValues1);
console.log(formNames1);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment