i'm working on a project where i have a series of passcodes that can only be used a predefined number of times. right now, i have two JS functions validating and an external JSON file housing data to verify against. my JS functions currently work to validate only one possible passcode. not sure how to approach scaling these functions out to iterate through the full JSON file. appreciate any direction.
thanks,
function validateCode() {
var txtCode = document.getElementById("INPUT_PASSCODE");
var results = txtCode.value.trim() != "" ? getRemainingCode(txtCode.value) : -1;
if (results == 0) {
alert('This code is no longer elegible');
txtCode.value = '';
txtCode.focus();
return false;
}
else if (txtCode.value.trim() != "" && results == -1) {
alert('Invalid code used');
txtCode.value = '';
txtCode.focus();
return false;
}
return true;
}
function getRemainingCode(code) {
var count = -1; //Not a valid code
jQuery.ajax({
url: '../codeCheck.aspx?Code=' + code + '&formhash=dfsgdfg',
dataType: 'json',
success: function (result) {
count = result.REMAINING;
if (isNaN(count))
count = -1;
},
async: false
});
return count;
}
JSON DATA
{
"Passcode_1":{
"ID":"sdfg3456",
"USED":"0",
"REMAINING":"1",
"TIMESTAMP":"4/30/2014 3:16:53 PM"
},
"Passcode_2":{
"ID":"jkhl765",
"USED":"0",
"REMAINING":"1",
"TIMESTAMP":"4/30/2014 3:16:53 PM"
},
"Passcode_3":{
"ID":"cvbn435",
"USED":"0",
"REMAINING":"1",
"TIMESTAMP":"4/30/2014 3:16:53 PM"
},
"Passcode_4":{
"ID":"345fgh",
"USED":"0",
"REMAINING":"1",
"TIMESTAMP":"4/30/2014 3:16:53 PM"
},
"Passcode_5":{
"ID":"5hrdd54",
"USED":"0",
"REMAINING":"1",
"TIMESTAMP":"4/30/2014 3:16:53 PM"
}
}