|Preg Match Problems (I Think!)|
First of all, great site and community ya'll have here. I have learned quite a bit in the past few weeks. I have come across something that has stumped me and I have no idea how to figure it out. By no means am I fluent in PHP, but I have dabbled with the code.
What this basically does is let a customer enter their account number. Once they enter it, it will search a table and return the account number and a temporary password. The account number ranges from all numbers, ie 91234124 to 22-12341 or 542-123456 etc... If a customer enters something other than a number, then I'd like it to return the message "Does not start with a number." If it can't find it in the database then, the message "It does not exist." I have gotten the numbers to work correctly, but whenever I try to enter an account number with a dash, it brings up the message that it doesn't exist. I think it has something to do with the preg_match. Also if I change the wording from = to LIKE in the $sql statement, i receive the a totally different account number and password than the one that is entered.
Can anybody help out with this? Thanks so much in advance!
$db=mysql_connect ("localhost", "----", "----") or die ('I cannot connect to the database because: ' . mysql_error());
$sql="SELECT acct, pw FROM `ouc` WHERE acct = $name";
echo "<div class='cbb red'><p><strong>This account number does not exist in our database, please enter the number exactly as it is printed on your bill. If you believe you have reached this message in error, please call us at ----- or email us at -----.</strong></p></div>";
echo "<div class='cbb green'><ul>\n";
echo "<li>" . "Account ID - " .$acct . "</a></li>\n";
echo "<li>" . "Temporary Password - " .$pw . "</a></li>\n";
echo "<div class='cbb gray'>Please write down your password as this will be the <strong>ONLY</strong> opportunity you have, then click below to validate and claim your account information. Once validated you will be able to log in, however you will <strong>NOT</strong> be able to use this screen again to find out your temporary password, so make sure you write it down.";
echo "<center><a href='-----.php?id=".$acct."'><img src='-----' border='0'></a></center><br/></div>";
echo "<div class='cbb red'><p><strong>Your account id must start with a number.</strong></p></div>";
Your regex looks right to me. You might want to test it separately to get at what is really going on here. Instead of working in this big block of code, you could just make a separate test file that says this:
if(preg_match("/^[0-9]+/", "-") echo 'it matches the dash!';
and once you figure out why it matches the dash, you can then do this:
if(preg_match("/^[0-9]+/", $_POST['name'])) 'it matched the dash via POST!';
and once you have that straight, you are ready to fix the script.
Debugging is easiest when you isolate a problem and then throw different solutions at it until it works.
Uh, 0-9 is zero to nine, numerically ... You need to add the dash to the possibilities of matches:
You also want it to be all numerical or dashes, so you should match start ^ to finish $
And you want it to match only if it starts with a number, so you need to eliminate the - match from the first character match
And keep in mind the preceding will match 9--------- which may not be desirable, so you might want to check for specific NNNN-NNNNNNNN patterns, like this, which would match 4 numbers followed by - followed by 8 numbers:
And, I'll let you have some fun with it from there...
Hi there kbrink43,
Welcome to WebmasterWorld!
Another alternative could be this (I am by no means a regex expert either):-
This will match all numerical data and the dash, as you can see from the array of answers, there is more than one way of doing this...
Thank you all for your help. I can't wait to get in there and start tinkering with the different ways you've showed me. You've all been a life savers, I will post back with the results!
Expanding the pre-dash/postdash to limits, say, a "between 4 and 6 before the dash and 8 and 12 after the dash"
Or is the dash optional? * means zero or more, ? means zero or one.
Which is really kinda funky, because it means "4-6 which can be followed by 8-12" which really means "4-18." :-)
4 or unlimited:
A usability thing, you might just "fix" it for them before your test so you don't have to throw an error. Remove anything but \d or dash.
$_POST['name'] = preg_replace('/[^\d\-]/','',$_POST['name']);
... will insure what you're testing is just numbers and dashes, then test against the format.