Hi all,I have an application collecting credit card data. Before sending the information out to the payment entity I am trying to make sure the information entered is, at least, valid. I already worked out the card number and cvv numbers but I am not so sure about the expiry date. The format I get the info is MMYY. So what I am doing is:---------------------------------------------------------------------------Exported from Notepad++-- Simple function to get current date and times functiongetdatetime(tz)localtz=tzor'America/New_York';localluatz=require'luatz';localfunctionts2tt(ts)returnluatz.timetable.new_from_timestamp(ts);endlocalutcnow=luatz.time();localtime_zone=luatz.get_tz(tz);localdatetime_raw=tostring(ts2tt(time_zone:localise(utcnow)));localyear,month,day,hour,min,sec,time_reminder=string.match(datetime_raw,"^(%d%d%d%d)%-(%d%d)%-(%d%d)[Tt](%d%d%.?%d*):(%d%d):(%d%d)()");returnyear,month,day,hour,min,sec;endlocalcurrent_year,current_month=getdatetime()-- Get current year/Month localcard_expiry_date='YYMM';-- In the app this actually get a value eg: 2204, 2301, 2010, etc. localcard_exp_year=string.sub(card_expiry_date,3,4)localcard_exp_month=string.sub(card_expiry_date,1,2)-- Extract the last two digits of the Year current_year=string.sub(current_year,3,4)-- Check month is valid if(card_exp_month<'01'orcard_exp_month>'12')thenprint("This is not a valid month")else-- Check date is this month or after if((card_exp_year<current_year)or(card_exp_year==current_yearandcard_exp_month<current_month))thenprint("Date cannot be before this month.")elseprint("All is good.")endend-------------------------------------------------------------------------
I do not know if this is the most elegant solution but it works. However it has a huge bug: it will fail at the end of the century. Since I only know the last two digits of the expiry date year, if a card expires in 2102 for instance and we were in 2099 my logic would wrongly reject the date (02 is less than 99).
I am very aware that me an my simple app will likely not be around by then but it bugs me to leave it like this.
Can anyone please suggest a proper way to do this validation?
Thank you!
--------------------------------------------------------Wilmar Pérez