/    Sign up×
Community /Pin to ProfileBookmark

Javascript age script problem

I have an age script running in the attachment below. It works fine for numbers such as 1 22 1980.

[CODE]if ((input.search(/b([1-9]|[12][0-9]|3[01])b/)!= -1) && (input.search(/b([1-9]|1[0-2])b/)!=-1) && (input.search(/b(19[0-9]{2}|200[0-9]|201[01])b/)!= -1)) {
function getBirth() {
var now = new Date();
var myMatchy = input.match(/b(19[0-9]{2}|200[0-9]|201[01])b/);
var myMatchm = input.match(/b([1-9]|1[0-2])b/)
var myMatchd = input.match(/b([1-9]|[12][0-9]|3[01])b/);
var year = myMatchy[0];
var month = myMatchm[0];
var day = myMatchd[0];
var today =
{
“year” : now.getFullYear(),
“month”: now.getMonth()+1,
“day” : now.getDate()
}

var age = today[“year”] – year;

if (month > today[“month”])
age–;
else if (month == today[“month”])
if (day > today[“day”]) age–
return age;
}
document.result.result.value = “Your age is “+getBirth()+”.”;
return true;}[/CODE]

However, if someone writes out the month it doesn’t. Such as January 22 1980. I did a work-around by inserting the month value. Highlighted below. But that means I would have to duplicate this lengthy code 12 times for each month of the year. Anybody know a better way to handle this? You can test it in the small attachment below. Any help would be appreciated.

[CODE]if ((input.search(/b([1-9]|[12][0-9]|3[01])b/)!= -1) && (input.search(/(january)/)!= -1) && (input.search(/b(19[0-9]{2}|200[0-9]|201[01])b/)!= -1)) {
function getJan() {
var now = new Date();
var myMatchy = input.match(/b(19[0-9]{2}|200[0-9]|201[01])b/);
var myMatchd = input.match(/b([1-9]|[12][0-9]|3[01])b/);
[COLOR=”Red”]var month = 0;[/COLOR]
var year = myMatchy[0];
var day = myMatchd[0];
var today =
{
“year” : now.getFullYear(),
“month”: now.getMonth()+1,
“day” : now.getDate()
}

var age = today[“year”] – year;

if (month > today[“month”])
age–;
else if (month == today[“month”])
if (day > today[“day”]) age–
return age;
}
document.result.result.value = “Your age is “+getJan()+”.”;
return true;}[/CODE]

[upl-file uuid=75660092-a659-4f5a-9d32-10cf228ed5fa size=3kB]aademo.txt[/upl-file]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 25.2011 — January is 0 in java Script Date() object
Copy linkTweet thisAlerts:
@chriscc17authorFeb 25.2011 — Ok, thanks I corrected that part. Any ideas how to solve my problem other than duplicating the code 12 times for each month?
Copy linkTweet thisAlerts:
@WolfShadeFeb 25.2011 — Don't give the option of free-style entry by using a text field; use a SELECT, instead, and populate it like:

01 January

02 February

03 March

.. etc.

You can assign whatever value to it that you like (0 or 1, or "January").

^_^
Copy linkTweet thisAlerts:
@chriscc17authorFeb 25.2011 — I understand the use of a form, menu etc. This however is an attempt to parse the information through natural language. I understand all the problems associated with that. My questions is very specific to this one problem. Can I modify the script in a way to handle written words.

Something like this within the body of the script so it can calculate an answer.

var myMatchy = input.match(/b(19[0-9]{2}|200[0-9]|201[01])b/);

var myMatchd = input.match(/b([1-9]|[12][0-9]|3[01])b/);

var myMatchm = input.match(/(january|february|march)/)

[COLOR="Red"]if (myMatchm == (january) {

var month = 0; }

if (myMatchm == (february) {

var month = 1; }

if (myMatchm == (march) {

var month = 2; } [/COLOR]


var year = myMatchy[0];

var day = myMatchd[0];
Copy linkTweet thisAlerts:
@KorFeb 25.2011 — Don't give the option of free-style entry by using a text field; use a SELECT, instead, and populate it like:

01 January

02 February

03 March

.. etc.

You can assign whatever value to it that you like (0 or 1, or "January").

^_^[/QUOTE]

Agree. Never let the user the possibility to enter something wrong. When it comes about dates, use 3 list boxes with year, month, date pre-set from server side.
Copy linkTweet thisAlerts:
@chriscc17authorFeb 25.2011 — I guess it wasn't clear in my original post that the attachment I enclosed is a chatterbot and I apologize for that. This is an attempt to answer the question often posed to the bot about age. So the format is set. No forms, menus etc but only a single input box.

I was just looking to see if someone had a solution to this specfic issue. If not I can always have the bot reply with please re-submit the questions in a numerical format like 1 22 1980 but that's no fun. ?
Copy linkTweet thisAlerts:
@KorFeb 27.2011 — There is no possibility to distinguish the American way (MM-DD-YYYY) from European way (DD-MM-YYYY) in many situations. You will never know whether the user's input 02-03-2011 means the 2nd of March 2011 or the 3rd of February 2011. The fact that you want to use a single input box [I]is the problem itself[/I].
×

Success!

Help @chriscc17 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...