Web Membership Profile - Error checking a serial number

Re: [cricket7475] Web Membership Profile - Error checking a serial number

By Dave - October 10, 2012

Hi cricket7475,

This would probably be a good use for regular expressions:
http://webcheatsheet.com/php/regular_expressions.php

How about something like this:

// serial error checking
$_REQUEST['serial'] = preg_replace("/[^\da-z]/i", '', @$_REQUEST['serial']); // remove chars that aren't a digit or a letter
$isValidSerial = preg_match("/^\d\d[a-z]\d\d\d\d\d\d$/i", $_REQUEST['serial']); // valid format: 2 numbers, 1 letter, 6 numbers
if (!$_REQUEST['serial']) { $errorsAndAlerts .= "No serial number entered!<br/>\n"; }
elseif (!$isValidSerial) { $errorsAndAlerts .= "You must enter a valid serial number, example: 11A111111!<br/>\n"; }
elseif ($_REQUEST['serial'] == '11A111111') { $errorsAndAlerts .= "Please enter your actual serial number, not the example one!<br/>\n"; }


Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Web Membership Profile - Error checking a serial number

Hi Dave,

Thanks so much for this - I had been looking at preg_match as an option but wasn't sure how to proceed. This and the resource link are extremely helpful.

I plugged in the following and it's working brilliantly:
// serial error checking
$_REQUEST['serial'] = preg_replace("/[^\da-z]/i", '', @$_REQUEST['serial']); // remove chars that aren't a digit or a letter
$isValidSerial = preg_match("/^\d\d[a-z]\d\d\d\d\d\d$/i", $_REQUEST['serial']); // valid format: 2 numbers, 1 letter, 6 numbers
if (!$_REQUEST['serial']) { $errorsAndAlerts .= "No serial number entered!<br/>\n"; }
elseif (!$isValidSerial) { $errorsAndAlerts .= "You must enter a valid serial number.<br/>\n"; }


As much as I would like to give our users some hints we're concerned about competitors accessing info only meant for product users. So I get to be a generic as possible with my error messages.

Thanks again - your support is always awesome!

Cricket7475