Page 1 of 1

Check for consecutive numbers/letters

Posted: Wed Jul 19, 2006 5:55 pm
by loveccb
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I'm trying to prevent against users entering consecutive numbers in the phone number, and consecutive letters in the first and last name on a form.

I want to prevent a user from typing 717-222-3124 or 717-531-1111 (as examples)

Here's what I have for the phone numbers.

Code: Select all

$phone1 = 717
$phone2 = 818
$phone3 = 3040

$pattern1 = "/^\d{3}$/";
$pattern2 = "/\d{4}/";

           if (preg_match($pattern1, $phone2, $matches))  {
             echo "$phone2 Phone 2 Invalid<br>";
           }
           else {
             echo "$phone2 Phone 2 Valid<br>";
           }

           if (preg_match($pattern2, $phone3, $matches))  {
             echo "$phone3 Phone 3 Invalid<br>";
           }
           else  {
             echo "$phone3 Phone 3 Valid<br>";
           }
The problem is, it ALWAYS returns Invalid no matter what number I use
Any ideas? As a follow up, I'd like to do this for numbers to.

Many thanks!


Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 24, 2006 3:04 pm
by tcsoft
the manual is your friend
preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match.
when i understand you right, $phone1, ... represent valid numbers.

then it must say:

Code: Select all

if( preg_match( $pattern1, $phone2, $matches ) ) 
  echo "$phone2 Phone 2 valid<br>";
else
  echo "$phone2 Phone 2 invalid<br>";