Skip to content

Instantly share code, notes, and snippets.

@MattWilliamsDev
Created November 20, 2014 22:08
Show Gist options
  • Select an option

  • Save MattWilliamsDev/060df5dd554b13b9d5bb to your computer and use it in GitHub Desktop.

Select an option

Save MattWilliamsDev/060df5dd554b13b9d5bb to your computer and use it in GitHub Desktop.
$( document ).ready(function() {
getFormJson(run_after);
});
var $ar4ec = $("#ar4ec").validate({
// Rules for form validation
rules : {
exemption : {
required : true
},
a1 : {
required : true,
digits : true,
max: 9
},
b1 : {
required : true,
digits : true,
max: 9
},
c1 : {
required : true,
digits : true,
max: 9
},
two : {
required : true,
digits : true,
max: 9
},
three : {
required : true,
digits : true,
max: 9
},
four : {
required : true,
digits : true,
max: 99999
},
five : {
required : true
},
status : {
required : true
},
signature : {
required : true
},
date : {
required : true
}
},
// Messages for form validation
messages : {
exemption : {
required : 'Select one please.'
},
a1 : {
required : 'Fill out or put zero please.',
digits : 'Digits only please.'
},
b1 : {
required : 'Fill out or put zero please.',
digits : 'Digits only please.'
},
c1 : {
required : 'Fill out or put zero please.',
digits : 'Digits only please.'
},
two : {
required : 'Fill out or put zero please.',
digits : 'Digets only please.'
},
three : {
required : 'Fill out or put zero please.',
digits : 'Digits only please'
},
four : {
required : 'Fill out or put zero please.'
},
five : {
required : 'Please Select one.'
},
status : {
required : 'Please select one.'
},
signature : {
required : 'Fill out name'
},
date : {
required : 'Can not be blank'
}
},
// Ajax form submition
submitHandler : function(form) {
$(form).ajaxSubmit({
success : function() {
// validation worked here so get the form by elements
var tiid = getParameterByName('tiid');
var formData = getFormData();
formData = JSON.stringify( formData );
console.log(formData);
$.post('/ajxep/_putFormData.php', {tiid:tiid,data:formData}, function(data, textStatus, xhr) {
/*optional stuff to do after success */
});
}
});
},
// Do not change code below
errorPlacement : function(error, element) {
error.insertAfter(element.parent());
console.log(error);
}
});
function sumItUp(){
var a1 = $("#a1").val();
var b1 = $("#b1").val();
var c1 = $("#c1").val();
var two = $("#two").val();
var total = 0;
total = Number(a1) + Number(b1) + Number(c1) + Number(two);
$("#three").val(total);
}
var init = function() {
$( "input[name=a1]" ).change(function() {
$("#b1").val(0);
$("#c1").val(0);
sumItUp();
});
// another way to do the above but doesn't change the functionality. :(
// $( "input[name=a1]" ).on("change" , function() {
// $("#b1").val(0);
// $("#c1").val(0);
// sumItUp();
// });
$( "input[name=b1]" ).change(function() {
$("#a1").val(0);
$("#c1").val(0);
sumItUp();
});
$( "input[name=c1]" ).change(function() {
$("#a1").val(0);
$("#b1").val(0);
sumItUp();
});
$( "input[name=two]" ).change(function() {
sumItUp();
});
$( "input[name=three]" ).change(function() {
sumItUp();
});
$('#date').datepicker({
dateFormat : 'yy-mm-dd',
prevText : '<i class="fa fa-chevron-left"></i>',
nextText : '<i class="fa fa-chevron-right"></i>',
maxDate : 0,
minDate : -30
});
// radio on change event so we know what to put in the fields
$('input[name=exemption]').change(function() {
// alert(this.value);
if ( $(this).val() === "a") {
// correct the stuff.
$("#a1").val('1');
$("#b1").val('0');
$("#c1").val('0');
} else if ( $(this).val() === "b") {
$("#a1").val('0');
$("#b1").val('2');
$("#c1").val('0');
} else if ( $(this).val() === "c") {
$("#a1").val('0');
$("#b1").val('0');
$("#c1").val('2');
}
sumItUp();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment