Alpha Video Training
Results 1 to 26 of 26

Thread: Calculate age as at January 1st !!!

  1. #1
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Calculate age as at January 1st !!!

    Let me try and explain this one !!!!

    My application needs to validate the "age" of a competitor to be eligible to enter a race class [category i.e. OR1].

    The age to enter the OR1 class is 16. However this is 16 years of age on the 1st of January. If the competitor is already/still 16 on the on the 1st of January, then he can enter the race.

    BUT !!!

    If he turned 17 BEFORE the 1st of January then he is not allowed to enter the race and if he turned 17 any other day of the year after the 1st of January, then he can still enter.

    MY question;
    How do I bring the 1st of January into the equation when his age is calculated and he enters TODAY [any date after the 1st of January].

    Confused, yip I am.

    16 years of age on the 1st of January is the marker/threshold. If he turns 17 on the 2nd of January then he can still enter the OR1 class as his entry age is calculated as on the 1st of January. Damn this is confusing.

  2. #2
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    hello

    the age function takes both arguments, date of birth and as of date.
    can you use that to calculate?
    for example this is done in the interactive window

    Code:
    dim date_of_birth as d
    dim on_the_day as d
    date_of_birth = {10/02/1949}
    on_the_day = {01/01/2013}
    ?age(date_of_birth)
    = 64
    
    ?age(date_of_birth,{01/01/2000})
    = 50
    
    
    ?age(date_of_birth,on_the_day)
    = 63
    Last edited by GGandhi; 11-28-2013 at 06:19 PM.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  3. #3
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,732

    Default Re: Calculate age as at January 1st !!!

    I think this is correct... have a look...

    http://www.youtube.com/watch?v=dJhwoR749o0

    You don't really need both these functions but here they are. Both functions take a parameter - the Date of Birth. The calculate_age does just that... calculates the age as of today. The calculate_age_as_of calculates the age as of a certain date... in this case Jan. 1 of the next year. I found the calcs on StackOverflow and adjusted a bit.

    Code:
    function calculate_age(currDOB)
    {
    
    	var dob = new Date(currDOB);
        var birth_year = dob.getFullYear();
        var birth_month = dob.getMonth();
        var birth_day = dob.getDate();
    
        var today_date = new Date();
        var today_year = today_date.getFullYear();
        var today_month = today_date.getMonth();
        var today_day = today_date.getDate();
        var age = today_year - birth_year;
    
        if ( today_month < (birth_month))
        {
            age--;
        }
        if (((birth_month) == today_month) && (today_day < birth_day))
        {
            age--;
        }
        return age;
    }
    Code:
    function calculate_age_as_of(currDOB)
    {
    
    	var dob = new Date(currDOB);
        var birth_year = dob.getFullYear();
        var birth_month = dob.getMonth();
        var birth_day = dob.getDate();
        
        var today_date = new Date();
        var today_year = today_date.getFullYear();
        today_year = today_year + 1;
        var today_month = 0; //remember this is zero based
        var today_day = 1;
        var age = today_year - birth_year;
    
        if ( today_month < (birth_month))
        {
            age--;
        }
        if (((birth_month) == today_month) && (today_day < birth_day))
        {
            age--;
        }
        return age;
    }
    The onClick events runs this code...

    Code:
    var currDOB = {dialog.Object}.getValue('currDOB');
    var currAge = calculate_age(currDOB);
    {dialog.Object}.setValue('currAge', currAge);
    
    
    var ageAsOf = calculate_age_as_of(currDOB);
    {dialog.Object}.setValue('ageAsOfNewYear', ageAsOf);

  4. #4
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    other way around
    if you have a field date_of_birth onChange event can call a javascript that checks the eligibility by

    Code:
    if age(date_of_birth,ctod("1/1/"+year(date()))) > 16
    function_name = "alert('You are not eligible to enter the event!');"
    end if
    looks simple
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  5. #5
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Wow,

    Thanks for the feedback. There is light at the end of the tunnel [huge tunnel in my case]

    Below is the code I run on a radio button called 'CompAMClass1E'. This code runs on the client side immediate validation on a grid and it works great.
    The 'AgeN' field has the age in days calculated by code placed in the SQL 'age' field. Gandi, this is the same code you assisted me with a week or what ago and it works great.

    So, I have an ageN field with the age being displayed correctly. How do I coincide your code with this code below

    var rn = {grid.object}._selectedRow;

    var AgeN = {grid.object}.getValue('G','AGE',rn);
    var CompA = {grid.object}.getValue('G','COMPETITIONA',rn);
    var RaceType = {grid.object}.getValue('G','SENIORJUNIOR',rn);

    var SOR1MinAgeN = 16;

    var CompAMClass1 = {grid.object}.getValue('G','CompAMClass1',rn);

    if(CompA == 'Senior National' && CompAMClass1 == 'OR1' && CompAMClass1E == 'Yes')
    {
    if(AgeN < SOR1MinAgeN)
    {
    return 'Age must be older or equal to 16 for OR1 Class';
    }
    }
    return false;

  6. #6
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Oh, In my scenario above, I have the running age. I need to bring 1st of Jan into it as age by date

  7. #7
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    hello
    if I look at your code
    AgeN is age in number of days
    SOR1MinAge age in numbers of years
    cannot be compared to give you what you want.
    if you look at your grid and how it is constructed you will see date of birth. if you do then

    var dateOFBirth = {grid.object}.getValue('G','DATE_OF_BIRTH',rn);
    if (age(dateOfBirth,ctod("1/1/"+year(date()))) < 16, "You must be older or equal to 16 for OR1 Class"," ")

    will probably work
    Last edited by GGandhi; 11-29-2013 at 06:44 AM.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  8. #8
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Hi Gandhi,

    I tried it as such, but validation does not take place.

    Could you perhaps explain to me what this all means so I can try and understand the function rather than just copy and paste it.
    This is how I changed it;

    var DOB = {grid.object}.getValue('G','DOB',rn);
    if (AgeN(DOB,ctod("1/1/"+year(date()))) < 16)

  9. #9
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Sorry I submitted the response before I completed the above.

    var DOB = {grid.object}.getValue('G','DOB',rn);

    if (AgeN(DOB,ctod("1/1/"+year(date()))) < SOR1MinAgeN)
    {
    return 'Too Young for OR1 class'
    }

  10. #10
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Sorry,

    I changed AgeN to age as per your example, but it still does not validate.

  11. #11
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Hi Dave

    I'm trying all the methods provided. Your video shows clearly what happens.

    Question;
    I have fields DOB & Age in my grid. Currently age is calculated from sql field formula [in sql].

    Must I remove the sql formula and then use your function to calculate the age when the competitor clicks the radio button on "Yes"

    I think my issue is, or rather I KNOW my issue is WHERE to run this function from and how to structure it for my client side immediate validation

  12. #12
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    okay
    would you mind posting a screeshot of the grid so as to know how it is constructed.
    or
    is this a form that competitor fills out with several races listed as check box and they click to enroll for various races?
    and you want to validate when they check the checkboxes?
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  13. #13
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Nope Gandhi, its a "form" grid with the respective fields on the grid.

    To choose the race classes the competitor needs to click a radio button "yes" or "no", no being the default value.

    On clicking "yes" the immediate field validation runs. Everything works and the validations work great, however, I need to validate the age as at the 1st of January.

    My current age field is calculated by the sql field formula (datediff(day,[DOB],getdate())/(365.25)) and is loaded as soon as the grid retrieves the info from the competitor table. This is correctly as you stated, age in days and not a date format.

    Below is the full validation code for one of the buttons. You will see the age, DOB fields, however, i'm actually only using the age field for validation as sql already did that one. Every "age" validation i'll need to change to what this thread is about.

    var rn = {grid.object}._selectedRow;

    var AgeN = {grid.object}.getValue('G','AGE',rn);
    var Sex = {grid.object}.getValue('G','SEX',rn);
    var CompType = {grid.object}.getValue('G','COMPETITIONTYPE',rn);
    var EventType = {grid.object}.getValue('G','EVENTTYPE',rn);
    var CompA = {grid.object}.getValue('G','COMPETITIONA',rn);
    var RaceType = {grid.object}.getValue('G','SENIORJUNIOR',rn);
    var EngineSize = {grid.object}.getValue('G','ENGINCAPACITY',rn);

    var SNMCMinAgeN = 16;
    var SNMSenMinAgeN = 38;
    var SNMMasMinAgeN = 46;
    var SNMSCMaxAgeN = 18;
    var JNMOpenMinAgeN = 15;
    var JNMOpenMaxAgeN = 17;
    var JNM125MinAgeN = 13;
    var JNM125MaxAgeN = 17;
    var JNMSen85MinAgeN = 13;
    var JNMSen85MaxAgeN = 15;
    var JNMJun85MinAgeN = 8;
    var JNMJun85MaxAgeN = 12;
    var JNM65MinAgeN = 7;
    var JNM65MaxAgeN = 11;
    var SOR1MinAgeN = 18;
    var SOR3MinAgeN = 16;
    var SOR2MinAgeN = 18;
    var SHighMinAgeN = 14;
    var SHighMaxAgeN = 16;

    var CompAMClass1 = {grid.object}.getValue('G','CompAMClass1',rn);
    var CompAMClass2 = {grid.object}.getValue('G','CompAMClass2',rn);
    var CompAMClass3 = {grid.object}.getValue('G','CompAMClass3',rn);
    var CompAMClass4 = {grid.object}.getValue('G','CompAMClass4',rn);
    var CompAMClass5 = {grid.object}.getValue('G','CompAMClass5',rn);
    var CompAMClass6 = {grid.object}.getValue('G','CompAMClass6',rn);
    var CompAMClass7 = {grid.object}.getValue('G','CompAMClass7',rn);
    var CompAMClass8 = {grid.object}.getValue('G','CompAMClass8',rn);
    var CompAMClass1E = {grid.object}.getValue('G','CompAMClass1E',rn);
    var CompAMClass2E = {grid.object}.getValue('G','CompAMClass2E',rn);
    var CompAMClass3E = {grid.object}.getValue('G','CompAMClass3E',rn);
    var CompAMClass4E = {grid.object}.getValue('G','CompAMClass4E',rn);
    var CompAMClass5E = {grid.object}.getValue('G','CompAMClass5E',rn);
    var CompAMClass6E = {grid.object}.getValue('G','CompAMClass6E',rn);
    var CompAMClass7E = {grid.object}.getValue('G','CompAMClass7E',rn);
    var CompAMClass8E = {grid.object}.getValue('G','CompAMClass8E',rn);

    /* CompAMClass1 validation */
    if(CompA == 'Senior Club')
    {
    if(AgeN < SHighMinAgeN)
    {
    return 'Your Age is below the required Limit for a Senior Club Event !!!';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'OR1' && CompAMClass1E == 'Yes')
    {
    if(AgeN < SOR1MinAgeN)
    {
    return 'Age must be older or equal to 18 for OR1 Class';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'OR1' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Bigger Than 251cc, 2-Stroke' && EngineSize != 'Bigger Than 401cc, 4-Stroke' && EngineSize != 'Bigger Than 201cc, 2-Stroke' && EngineSize !=
    'Smaller Than 251cc, 2-Stroke' && EngineSize != 'Smaller Than 201cc, 2-Stroke' && EngineSize != 'Smaller Than 251cc, 4-Stroke')
    {
    return 'Engine Size must be Bigger Than 200cc, 2-Stroke OR Bigger Than 250cc, 4-Stroke';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'OR2' && CompAMClass1E == 'Yes')
    {
    if(AgeN < SOR2MinAgeN)
    {
    return 'Age must be older or equal to 18 for OR2 Class';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'OR2' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Bigger Than 201cc, 2-Stroke' && EngineSize != 'Smaller Than 251cc, 2-Stroke' && EngineSize != 'Smaller Than 201cc, 2-Stroke' && EngineSize !=
    'Smaller Than 251cc, 4-Stroke' && EngineSize != 'Bigger Than 251cc, 4-Stroke')
    {
    return 'Engine Size must be Up to 250cc, 2-Stroke and 400cc, 4-Stroke';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'OR3' && CompAMClass1E == 'Yes')
    {
    if(AgeN < SOR3MinAgeN)
    {
    return 'Age must be older or equal to 16 for OR3 Class';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'OR3' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Smaller Than 201cc, 2-Stroke' && EngineSize != 'Smaller Than 251cc, 4-Stroke')
    {
    return 'Engine Size must be Up to 200cc, 2-Stroke and 250cc, 4-Stroke';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'Senior' && CompAMClass1E == 'Yes')
    {
    if(AgeN < SNMSenMinAgeN)
    {
    return 'Age must be older or equal to 38 for Senior Class';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'HighSchool' && CompAMClass1E == 'Yes')
    {
    if(AgeN < SHighMinAgeN || AgeN > SHighMaxAgeN)
    {
    return 'Age must be between 14 & 16 for Highschool Class';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'HighSchool' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Smaller Than 201cc, 2-Stroke' && EngineSize != 'Smaller Than 251cc, 4-Stroke')
    {
    return 'Engine Size must be Smaller Than 201cc, 2-Stroke OR Smaller Than 251cc, 4-Stroke';
    }
    }

    if(CompA == 'Senior Club' && CompAMClass1 == 'Master' && CompAMClass1E == 'Yes')
    {
    if(AgeN < SNMMasMinAgeN)
    {
    return 'Age must be older or equal to 46 for Master Class';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'Silver' && CompAMClass1E == 'Yes')
    {
    if(AgeN < SNMSCMinAgeN && AgeN > SNMSCMaxAgeN)
    {
    return 'Age must be between than 16 and 18 for Silver class';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'Silver' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Smaller Than 201cc, 2-Stroke' && EngineSize != 'Smaller Than 251cc, 4-Stroke')
    {
    return 'Engine Size must be Smaller Than 201cc, 2-Stroke OR Smaller Than 251cc, 4-Stroke';
    }
    }
    if(CompA == 'Senior Club' && CompAMClass1 == 'Ladies' && CompAMClass1E == 'Yes')
    {
    if(Sex != 'Female')
    {
    return 'Only Ladies can enter this class';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == 'Open' && CompAMClass1E == 'Yes')
    {
    if(AgeN < JNMOpenMinAgeN || AgeN > JNMOpenMaxAgeN)
    {
    return 'Junior Open Class Age must be between 15 & 17';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == 'Open' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Smaller Than 201cc, 2-Stroke' && EngineSize != 'Smaller Than 251cc, 4-Stroke')
    {
    return 'Engine Size must be Smaller than 201cc 2-Stroke OR Smaller Than 251cc 4-Stroke';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == '125cc' && CompAMClass1E == 'Yes')
    {
    if(AgeN < JNM125MinAgeN || AgeN > JNM125MaxAgeN)
    {
    return 'Junior Open Class Age must be between 13 & 17';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == '125cc' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Smaller Than 126cc, 2-Stroke')
    {
    return 'Engine Size must be Smaller Than 126cc, 2-Stroke';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == 'Senior 85' && CompAMClass1E == 'Yes')
    {
    if(AgeN < JNMSen85MinAgeN || AgeN > JNMSen85MaxAgeN)
    {
    return 'Junior Open Class Age must be between 13 & 15';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == 'Senior 85' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Smaller Than 106cc, 2-Stroke' && EngineSize != 'Smaller Than 151cc, 4-Stroke')
    {
    return 'Engine Size must be Smaller Than 106cc, 2-Stroke OR Smaller Than 151cc, 4-Stroke';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == 'Junior 85' && CompAMClass1E == 'Yes')
    {
    if(AgeN < JNMJun85MinAgeN || AgeN > JNMJun85MaxAgeN)
    {
    return 'Junior Open Class Age must be between 8 & 12';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == 'Junior 85' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Smaller Than 106cc, 2-Stroke' && EngineSize != 'Smaller Than 151cc, 4-Stroke')
    {
    return 'Engine Size must be Smaller Than 106cc, 2-Stroke OR Smaller Than 151cc, 4-Stroke';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == 'Junior 65' && CompAMClass1E == 'Yes')
    {
    if(AgeN < JNM65MinAgeN || AgeN > JNM65MaxAgeN)
    {
    return 'Junior Open Class Age must be between 7 & 11';
    }
    }
    if(CompA == 'Junior Club' && CompAMClass1 == 'Junior 65' && CompAMClass1E == 'Yes')
    {
    if(EngineSize != 'Smaller Than 66cc, 2-Stroke' && EngineSize != 'Smaller Than 81cc, 4-Stroke')
    {
    return 'Engine Size must be Smaller Than 66cc, 2-Stroke OR Smaller Than 81cc, 4-Stroke';
    }
    }
    return false;

  14. #14
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    var AgeN = {grid.object}.getValue('G','AGE',rn);
    var Sex = {grid.object}.getValue('G','SEX',rn);
    var CompType = {grid.object}.getValue('G','COMPETITIONTYPE',rn);
    var EventType = {grid.object}.getValue('G','EVENTTYPE',rn);
    var CompA = {grid.object}.getValue('G','COMPETITIONA',rn);
    var RaceType = {grid.object}.getValue('G','SENIORJUNIOR',rn);
    var EngineSize = {grid.object}.getValue('G','ENGINCAPACITY',rn);
    I do not see date_of_birth
    do you have date of birth in the grid? ( I know you have age)
    if you have date_of_birth in the grid then try the following
    dim Date_Of_Brith = {grid.object}.getValue('G','DATE_OF_BIRTH',rn)
    dim AgeN as n
    AgeN = age(Date_Of_Birth, ctod("01/01/"+year(date())))
    then keep the rest of your code and see if it works.
    I am only changing how AgeN is calculated.
    Last edited by GGandhi; 12-03-2013 at 09:31 AM.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  15. #15
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Do I insert this code also with the above?

  16. #16
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Yip, I do have a DOB field which is a datetime field as required by the sql formula

  17. #17
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    Yip, I do have a DOB field which is a datetime field as required by the sql formula
    what formula?
    date of birth can be defined as date in your sql table definition not necessary to have datetime.
    or get the date from the datetime, then just eliminate the code you have for AgeN and insert my code and see what happens.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  18. #18
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Quote Originally Posted by GGandhi View Post
    what formula?
    date of birth can be defined as date in your sql table definition not necessary to have datetime.
    or get the date from the datetime, then just eliminate the code you have for AgeN and insert my code and see what happens.
    Hanghi,

    The window just carry on "loading" No reaction.

    I removed my var agen code as you said.

    Maybe I should replace the DOB DateTime field in sql with a date field.

  19. #19
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Sorry, the age field has the formula not dob

  20. #20
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    Maybe I should replace the DOB DateTime field in sql with a date field.
    date of birth can be defined as date in your sql table definition not necessary to have datetime.
    that is what I suggested in post #17

    if you have date of birth as date not datetime
    then AgeN can be calculated as of January 1st of any given year. then you do all other maths based on AgeN

    also can you post your screencapture image of your grid? (just to see what is involved.)
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  21. #21
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,732

    Default Re: Calculate age as at January 1st !!!

    Hey Cornelius,

    I think part of the confusion is that you're using Client-side only Immediate validation Javascript. However, I think Govindan believes you are using Server-side validation and is therefore posting XBasic code to calculate age as of Jan 1. Just a mis-communication.

    If you want to stay in Client-side (and you should, if possible 'cause you don't have to make a trip to the server) then you'll need to use Javascript to calc the 'Age As Of'. Have a look at this...

    http://www.youtube.com/watch?v=_K6BQDCpyUU

    If the validation process you're using is not what I've outlined above then post, specifically, what you're using... and where. Although you can see the code I'm using in the video, here it is. I've commented out only that code I didn't need. Most of it is as you posted.

    Code:
    var rn = {grid.object}._selectedRow;
    
    //var AgeN = {grid.object}.getValue('G','AGE',rn);
    
    var DOB = {grid.object}.getValue('G','COMPDOB',rn);
    var CompA = {grid.object}.getValue('G','COMPETITIONA',rn);
    //var RaceType = {grid.object}.getValue('G','SENIORJUNIOR',rn);
    
    var AgeN = calculate_age_as_of(DOB);
    var SOR1MinAgeN = 16;
    
    var CompAMClass1 = {grid.object}.getValue('G','CompAMClass1',rn);
    var CompAMClass1E = {grid.object}.getValue('G','CompAMClass1E',rn);
    
    if(CompA == 'Senior National' && CompAMClass1 == 'OR1' && CompAMClass1E == 'Yes')
    {	
    
    if(AgeN < SOR1MinAgeN)
    {
    return 'Age must be older or equal to 16 for OR1 Class';
    }
    }
    return false;
    Hope that helps. If not... we'll try again.

  22. #22
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    hello

    I just want to point out to you if you already did not know, that after all checking anybody can still insert the record into the database.
    the better way to do will be get all information and use canInsertRecord server side script to catch all those ifs and cancel insertion at that time.
    I can post en example if you want. but I am sure you can do it.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

  23. #23
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Damn, you're good !!!

  24. #24
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Gandhi,

    I have tested your code and it works great.

    Just a question. I might be doing something wrong still as it validates on the year correctly, but it does not seem to take the 1st of January into account.

    We need to validate the age as at the 1st of January each year to be eligible for the class. MSA [Motorsport South Africa] rules state that the age threshold for ANY class entry must be as on the 1st of January each year.

    If my birthday is the 2nd of Jan and I turn 16 then, I will be to young for the OR1 class for the rest of the year. If I turn 16 on the 1st or before the 1st, then I can enter the OR1 class.

  25. #25
    Guest
    Real Name
    Cornelius Laubscher
    Join Date
    Oct 2013
    Posts
    97

    Default Re: Calculate age as at January 1st !!!

    Quote Originally Posted by GGandhi View Post
    hello

    I just want to point out to you if you already did not know, that after all checking anybody can still insert the record into the database.
    the better way to do will be get all information and use canInsertRecord server side script to catch all those ifs and cancel insertion at that time.
    I can post en example if you want. but I am sure you can do it.
    Gandhi,
    Any example will be appreciated.

    Ps.
    I read on a thread somewhere you might have visited a friend with a houseboat. Did you go?
    My daughter is currently in Dallas doing student work and engaging the FBI. She is majoring in Criminology degree studies and has applied with them for acceptance. Are you far from Dallas?

  26. #26
    "Certified" Alphaholic
    Real Name
    Govindan Gandhi
    Join Date
    Aug 2008
    Location
    New York, NY
    Posts
    4,294

    Default Re: Calculate age as at January 1st !!!

    hello

    1. it is rather difficult to calculate age at the start of the year in sql and javascript but lot easier in alpha.
    2. you also do not want people able to insert data if there are any errors just like xdialog does.
    3. you also do not want anyone to update with a wrong data once they entered the correct data earlier.

    I have created couple of videos to show.
    if this is not what you want please discard the info.

    http://screencast.com/t/akdfjxYhCl

    http://screencast.com/t/t6u1GVmq

    http://screencast.com/t/JWojoc47yR3J

    http://screencast.com/t/AEuNGRk3

    edit: final video
    http://screencast.com/t/IQcDC4ykaU
    Last edited by GGandhi; 12-04-2013 at 09:46 AM.
    thanks for reading

    gandhi

    version 11 3381 - 4096
    mysql backend
    http://www.alphawebprogramming.blogspot.com
    ggandhi344@gmail.com
    Skype:ggandhi344@gmail.com
    1 914 924 5171

Similar Threads

  1. Calculate Age in a Dialog
    By Jberry in forum Application Server Version 11 - Web/Browser Applications
    Replies: 6
    Last Post: 01-18-2012, 10:51 PM
  2. Week number from 1st January
    By Mike Vance in forum Alpha Five Version 10 - Desktop Applications
    Replies: 2
    Last Post: 06-21-2011, 09:49 AM
  3. calculate age from 2 separate tables?
    By dlazenby in forum Alpha Five Version 9 - Desktop Applications
    Replies: 4
    Last Post: 09-15-2009, 09:14 PM
  4. Calculate age
    By Edgar Lewis in forum Alpha Five Version 4
    Replies: 2
    Last Post: 05-06-2001, 06:01 AM
  5. Calculate age
    By Joseph Mann in forum Alpha Five Version 4
    Replies: 16
    Last Post: 03-13-2001, 05:56 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •