0

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"
   }
}
unfollow
  • 225
  • 1
  • 5
  • 12
  • What is your question? What are you having a problem with? – lincolnk May 06 '14 at 17:48
  • basically, the way the code is structured, how do i check REMAINING for each potential Passcode section in the JSON. in otherwords, the user could submit any passcode from that list, i need to reduce the remaining number for each passcode section if it's used. – unfollow May 06 '14 at 17:50
  • do **NOT** use `async:false`, and read -> http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – adeneo May 06 '14 at 17:50
  • you're treating the json like a database? you should be using an actual database... because yes, you'll have to iterate large chunks of the file every time you want to look up something. – Marc B May 06 '14 at 17:50
  • doesn't a DB seem like overkill for something like this? it's only 5 possible options. – unfollow May 06 '14 at 17:52
  • thanks for the reference adeneo, i'll correct that. – unfollow May 06 '14 at 17:53

3 Answers3

1

To access an object you need loop through it via the for in loop.

for(var i in result)
{
    var count = result[i].REMAINING;
}
Jason Foglia
  • 2,414
  • 3
  • 27
  • 48
0

As it stands, you're accessing your JSON object incorrectly. Do this in your success callback:

for(var i in result)
{
    var count = result[i].REMAINING;
}
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
0

I have an observation about your problem:

You should have an array instead of a json. It will make sense as passcode_n is not adding any value to your codebase.

And here goes my soludion based on that observation:

Your array goes here:

[
   "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"
   }
]

Here goes the way to iterate it :

    (function(passcode){
      $.each(passcode_array, function(index, value){
        //validate passcode in function
        if(is_passcode_matched_and_valid(passcode, value)){
          //do things
        }
      });    
    })(passcode);
MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45