Forum Replies Created

Viewing 1 replies (of 1 total)
  • Here’s a helpful and relevant reply you can use for that thread:

    It looks like you’re on the right track with your script, but there are a couple of possible issues that could cause the calculator to not work within the Calculated Fields Form plugin:

    1. Smart Quotes: The code you posted uses curly quotes (‘fieldname3’), which are not valid in JavaScript. Make sure you replace them with straight quotes: 'fieldname3'.
    2. Field Format: Ensure that the date input format in your fieldname3 is consistently MM/DD/YYYY, as your script depends on this structure.
    3. Logic Order: You’re checking younger divisions first (e.g., U12), but that could misclassify someone born in 2009 as U12. You should check older divisions first, then move toward younger ones.

    Here’s a corrected and improved version of your script:

    (function() {
      var dobStr = field('fieldname3'); // Your DOB field name
      if (!dobStr) return '';
    
      // Parse MM/DD/YYYY format
      var parts = dobStr.split('/');
      if (parts.length !== 3) return 'Invalid date format';
    
      var month = parseInt(parts[0], 10) - 1;  // JS months zero-based
      var day = parseInt(parts[1], 10);
      var year = parseInt(parts[2], 10);
    
      var dobDate = new Date(year, month, day);
      if (isNaN(dobDate.getTime())) return 'Invalid date';
    
      // Cutoff dates (June 30 each year)
      var cutoff_U16 = new Date(2009, 5, 30);
      var cutoff_U15 = new Date(2010, 5, 30);
      var cutoff_U14 = new Date(2011, 5, 30);
      var cutoff_U13 = new Date(2012, 5, 30);
      var cutoff_U12 = new Date(2013, 5, 30);
    
      // Check from oldest to youngest
      if (dobDate <= cutoff_U16) return 'U16';
      if (dobDate <= cutoff_U15) return 'U15';
      if (dobDate <= cutoff_U14) return 'U14';
      if (dobDate <= cutoff_U13) return 'U13';
      if (dobDate <= cutoff_U12) return 'U12';
    
      return 'Not eligible';
    })();
    

    Also, if you’re working with age-related logic in forms or need more advanced age calculations — like calculating age down to the minute — you might find this helpful tool:
    👉 حساب العمر بالدقائق — an Arabic calculator that can compute age in detail.

    Let me know if you need help integrating this into your specific form setup!

Viewing 1 replies (of 1 total)