[Pro] Javascript: Calculate age from Social Security Number

How could I calculate if a person is to young to apply through my form?

One has to tell his/her social security number to apply for this sport team, but some parents do not read our age limit and apply anyway.

This is how the number is in my country:

170509-5012 (example for a person born today)

17=day of month, 05=month, 09=year, 5012=random number (OK, I know this is not a completely random number, there’s some rule behind it)

So how could I, with the help of JavaScript, calculate that this person is younger than five year old?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I haven’t tested this, but it ought to work okay.

function oldEnough(ssn, intMinimumAge){
	if (arguments.length < 2) return false;
	var cutoff = new Date();
	cutoff.setFullYear(cutoff.getFullYear() - intMinimumAge,  
cutoff.getMonth(), cutoff.getDate());
	var birthday = new Date();
	birthday.setYear(ssn.substr(4,6));
	birthday.setMonth(ssn.substr(2,4));
	birthday.setDate(ssn.substr(0,2));
	return (birthday <= cutoff);
}

One thing you may need to modify is the month conversion from the ssn.
If they are using 0-based months (0-11) then leave it alone, but if
they are using the more natural sounding 1-based month numbers (1-12),
then you will need to change the birthday.setMonth call to look like
this:

birthday.setMonth(ssn.substr(2,4) -1 );

Let me know how it works for you.

Walter

On May 17, 2009, at 3:13 PM, Egill Sigurdur wrote:

170509-5012 (example for a person born today)

17=day of month, 05=month, 09=year, 5012=random number (OK, I know
this is not a completely random number, there’s some rule behind it)


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

function oldEnough(ssn, intMinimumAge){
    if (arguments.length < 2) return false;
    var cutoff = new Date();
    cutoff.setFullYear(
        cutoff.getFullYear() - intMinimumAge, 
        cutoff.getMonth(), 
        cutoff.getDate()
    );
    var birthday = new Date();
    birthday.setYear(ssn.substr(4,6));
    birthday.setMonth(ssn.substr(2,4));
    birthday.setDate(ssn.substr(0,2));
    return (birthday <= cutoff);
}

Formatting from a Mail message got all weirded out for some reason…


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Ok, thanks, I just wasn’t aware that Javascript also had the substr function.

I just had to alter this slightly, the computer always thought that the year was 19"05", not 20"05", so I had to add '“20” + ’ to it, but now anyone born in the 20. century will be told he’s younger than five.



function oldEnough(ssn, intMinimumAge){
if (arguments.length < 2) return false;
var cutoff = new Date();
cutoff.setFullYear(
cutoff.getFullYear() - intMinimumAge,
cutoff.getMonth(),
cutoff.getDate()
);
var birthday = new Date();
birthday.setYear("20" + ssn.substr(4,2));
birthday.setMonth(ssn.substr(2,2));
birthday.setDate(ssn.substr(0,2));

var str='';
str+='Not old enough.';
document.write(str);

if(birthday <= cutoff) {
document.write(str);
}
}



[I am using the document.write for now, to know what’s going on, but also I don’t have a clue on how I’m gonna make this interact with the textbox]


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Yeah, there’s just no good way to do the end of the millennium start of the millennium thing with two numbers, but I’ve put a decent guess in there – nobody born in 1930 going to be signing up here, right?

As far as using it goes, I wouldn’t put the document.write stuff in there, just use the function to tell you yes or no, and write from that.

function oldEnough(ssn, intMinimumAge){
    if (arguments.length < 2) return false;
    var cutoff = new Date();
    cutoff.setFullYear(
        cutoff.getFullYear() - intMinimumAge, 
        cutoff.getMonth(), 
        cutoff.getDate()
    );
    var birthday = new Date();
    var y = ssn.substr(4,6);
    y = (parseInt(y,10) < 30) ? '20' + y : '19' + y;
    birthday.setFullYear(y);
    birthday.setMonth(ssn.substr(2,4));
    birthday.setDate(ssn.substr(0,2));
    return (birthday <= cutoff);
}

Once you’ve got that, you can use it by getting the value of your field and testing it.

<input name="ssn" type="text" 
    onblur="if(!oldEnough(this.value,5)) 
        alert(\"You’re a little short for a Stormtrooper, aren’t you?\")" />

You can also update the value of a DIV on the page if you want that Web 2 oh! look to it. If you’re going to do that, then use Prototype so you don’t have cross-browser issues.

//link prototype.js to the head of the page, then...
$('ssn').observe('change',function(evt){
    if(!oldEnough($F('ssn'),5)){
        $('messageDiv').update("You’re too young!");
    }else{
        $('messageDiv').update('');
    }
});

That should get you going…

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Wow, thanks, but since I pretty much am a beginner in JS, could you explain to me what’s wrong?

I get this working if I document.write oldEnough(“1301082102”,“5”), but not without the quotes.

I can’t get this to work with this.value. Web Inpector always says: “TypeError: Result of expression ‘str.substr’ [undefined] is not a function.”

Yes, I wan’t to do this with somthing like Prototype, but I suck at that.




If I substr(4,6) something, I get six numbers, starting from the fourth, so I just changed all the last substr numbers to 2, is that right??


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Hmmm. Okay, I guess there needs to be some type-casting in there.

On May 18, 2009, at 3:50 AM, Egill Sigurdur wrote:

Wow, thanks, but since I pretty much am a beginner in JS, could you
explain to me what’s wrong?

I get this working if I document.write oldEnough(“1301082102”,“5”),
but not without the quotes.

Bare numbers appear to JavaScript to be numbers first and foremost,
and in order to treat them like a string (to use substr()) you have to
“cast” them to a string using the toString() function. Wrapping them
in quotes also makes them into a string. I left this out of my
function, you could fix it there (and then it would work in both the
quoted and the unquoted form):

 function oldEnough(ssn, intMinimumAge){
     if (arguments.length < 2) return false;
     var ssn = ssn.toString();
     ...

I can’t get this to work with this.value. Web Inpector always
says: “TypeError: Result of expression ‘str.substr’ [undefined] is
not a function.”

This should also be fixed, because we will cast the value to string
explicitly before trying to treat it as a string.

Yes, I wan’t to do this with somthing like Prototype, but I suck at
that.

I find Prototype to be much easier than vanilla JavaScript, because
you don’t have to worry about the plumbing as much. There was a 6
month learning curve similar to the Himalayas, but then it levels off,
I promise.




If I substr(4,6) something, I get six numbers, starting from the
fourth, so I just changed all the last substr numbers to 2, is that
right??

My mistake here as well. JavaScript has a substring() function and a
substr() function, and I used the wrong one. You are correct – the
syntax for substr() is (string, offsetStart, numberOfCharacters). The
syntax for substring() is (string, offsetStart, offsetEnd).

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks a lot!!

I ended up using Scriptaculous, but you are The Awesomeness.

So far
this is what I’ve got. (you write the number in the Kennitala field and if you’re to young you’re told so)

Thank you.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options