homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Preg Match Problems (I Think!)

Msg#: 4286350 posted 4:37 pm on Mar 23, 2011 (gmt 0)

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!


if(preg_match("/^[0-9]+/", $_POST['name'])){

$db=mysql_connect ("localhost", "----", "----") or die ('I cannot connect to the database because: ' . mysql_error());
$sql="SELECT acct, pw FROM `ouc` WHERE acct = $name";
$acct =$row['acct'];
$pw =$row['pw'];

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 "</ul></div>";
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 "<br><br>";
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>";



5+ Year Member

Msg#: 4286350 posted 8:38 pm on Mar 23, 2011 (gmt 0)

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.

Good luck.


WebmasterWorld Senior Member themadscientist us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

Msg#: 4286350 posted 2:40 am on Mar 24, 2011 (gmt 0)

Uh, 0-9 is zero to nine, numerically ... You need to add the dash to the possibilities of matches:

preg_match("/^[0-9-]+/", $_POST['name']);

You also want it to be all numerical or dashes, so you should match start ^ to finish $

preg_match("/^[0-9-]+$/", $_POST['name']);

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

preg_match("/^[0-9][0-9-]+$/", $_POST['name']);

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:

preg_match("/^[0-9]{4}-[0-9]{8}$/", $_POST['name']);

And, I'll let you have some fun with it from there...


WebmasterWorld Senior Member 5+ Year Member

Msg#: 4286350 posted 11:16 am on Mar 24, 2011 (gmt 0)

Hi there kbrink43,

Welcome to WebmasterWorld!

Another alternative could be this (I am by no means a regex expert either):-

preg_match("/^[\d-]+/m", $_POST['whatever']);

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...



Msg#: 4286350 posted 2:03 pm on Mar 24, 2011 (gmt 0)

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!


WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member

Msg#: 4286350 posted 4:21 pm on Mar 24, 2011 (gmt 0)

Expanding the pre-dash/postdash to limits, say, a "between 4 and 6 before the dash and 8 and 12 after the dash"

preg_match("/^\d{4,6}-\d{8,12}$/", $_POST['name']);

Or is the dash optional? * means zero or more, ? means zero or one.

preg_match("/^\d{4,6}-?\d{8,12}$/", $_POST['name']);

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:

preg_match("/^\d{4,}$/", $_POST['name']);

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']);
preg_match("/^\d{4,6}-\d{8,12}$/", $_POST['name']);

... will insure what you're testing is just numbers and dashes, then test against the format.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved