Validating a 4-Digit year



if(strlen($ccexpyear != 4))
      $error_list[] = 'Please enter a 4-digit year.';

I’m passing 2007 as the var’s value and it’s failing this check. Does strlen() not work for numbers?

Thanks for any help.

And when I echo out strlen($ccexpyear) it spits out a 4.


nevermind. damn parenthesis in the wrong place.

Take the " != 4" out of the parentheses:

if (strlen($ccexpyear) != 4)

[edit: oops, looks like you figured that out already.]

By the way, it’d probably be better to use regular expressions instead.

Probably not. :slight_smile: In this case it’d be better to use number comparison:

if($ccexpyear<1000 || $ccexpyear>9999){

Always try to avoid the unnecessary data type conversion. And always try to get around with string functions if needed first, only then fall to regexp.

If your sticking with strlen at least make sure its numeric:

$cardexp = intval($_POST['cardexp']);
$cardleng = strlen($cardexp);
if($cardleng != 4)
//do whatever