?
Solved

looking for a fool proof method of converting date to age

Posted on 2003-12-06
4
Medium Priority
?
267 Views
Last Modified: 2013-12-24
looking for a fool proof method of converting date to age. BUT the input field must allow the user to either type the age or the date.

in other words if they type an age do nothing just save

if they type a date convert to age

if they type something else fix their error
0
Comment
Question by:kleigh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 12

Accepted Solution

by:
jyokum earned 2000 total points
ID: 9889941
> if they type something else fix their error
that's a pretty tall order. what if I type in "goober"? there's no way I could figure out what the person really meant.

this will at least handle the date or age... it will just display an error for anything else


<cfif isDefined('form.ageORdob')>
      <cfscript>
      error=0;
      if(IsDate(form.ageORdob) AND DateCompare(form.ageORdob,now()) lte 0) age = DateDiff('yyyy',form.ageORdob,now());
      else if(IsNumeric(form.ageORdob) and form.ageORdob gte 0) age = form.ageORdob;
      else error=1;
      </cfscript>
      <cfoutput>
      <cfif error>
      <p>The value you supplied (#form.ageORdob#) is invalid. Age must be a positive number or a valid date that does not occur in the future</p>
      <cfelse>
      <p>According to the value you supplied (#form.ageORdob#), you are #age# years old</p>
      </cfif>
      </cfoutput>
</cfif>

<form name="frmData" method="post">
Enter your age or birthdate: <input type="text" name="ageORdob"><br />
<input type="submit">
</form>
0
 
LVL 17

Expert Comment

by:anandkp
ID: 9890708
a option shld be given to the user to decide whether he wnats to input [age] OR [dob]

what i do is - get the dob from the user & calculate the age.
but if u think users dont wanna reveal their "full dob" - then u cld ask them to just enter "dd/mm" & let them enter age in a seperate input box

this way they decide how old they wanna appear on ur site & also give in their dob [depends if ur site uses it]

else there wld be seveeral workarounds - to chk for the numeric field & make sure user dosent type in something else ... but they all wont look elegant. id suggest a  change in flow [if u can - pls do it]

rest its ur call ...

let us know incase u need more help !

K'Rgds
Anand
0
 
LVL 11

Expert Comment

by:hart
ID: 9894644
to avoid all these checks...

give three drop downs in the form..
one for days, months and years..

now let the user select the day/month and year..

and all u have to do is calculate the age by using datediff function..

Regards
Hart
0
 
LVL 17

Expert Comment

by:Tacobell777
ID: 9895382
This will do what you want, it's not how I would handle it, but.......

<input type="text" name="age">

<!--- try and get the age from a date --->
<cftry>
      <cfset age = dateDiff("yyyy", form.age, now())>
      <!--- the value passed was not a valid date --->
      <cfcatch>
            <!--- see if an integer was entered --->
            <cfif val(form.age) NEQ 0>
                  <cfset age = form.age>
            <cfelse>
                  Sorry not a valid date or age.
            </cfif>
      </cfcatch>
</cftry>
<cfoutput>
#variables.age#
</cfoutput>
0

Featured Post

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question