-2

I have a website form that collects user details. The phone filed is awesome in that it validates the phone number and prevents anyone from leaving the field blank or typing in a word.

Here's how my regex looks for that field:

if (!preg_match('/^[0-9ext()+ -.:]{9,35}$/i', $Phone)){
$error.= "<li><font face=arial>Phone number is <em><strong>required</strong></em> if you're truly serious about generating $500/Day.  Enter full number with area code.  Also, you should use only the following characters: '+' , '0-9' , ' ( ' , ' ) ' , 'ext' , or 'x'.  Example, <strong>(123) 555-1212</strong></font><br><br>"; 
$errors=1;
}

The problem I'm getting is that some are entering bogus numbers, like "555-1212" or "5551212". How can I add to the regex above to throw back the error message if someone adds the fake "5551212" number?

Inputs phone nos:(should be matched with 5551212 as a substring)

1-800-555-1212
8585551212
(619) 555-1212

Thanks for your awesome help!

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Jaime
  • 21
  • 6
  • A character class is a list of characters or ranges, not words. The `-` also needs to be escaped in your character class, or moved to the start or end. – chris85 Apr 30 '17 at 23:10
  • So if I read the question correctly you want to disallow if the first 3 characters are `555`. If that is correct maybe `^(?!555)[0-9ext()+ .:-]{9,35}$` works? – chris85 Apr 30 '17 at 23:11
  • Please add a couple of examples of the best case input. and another couple of bad cases. im unsure of the input you want to validate – Jason Joslin Apr 30 '17 at 23:15
  • 1
    seems the OP's left or waiting on answers rather than replying to comments. – Funk Forty Niner Apr 30 '17 at 23:23
  • Wow... didn't expect comments so fast. The main problem is that Stackoverflow doesn't email me to let me know someone has commented. Anyway, someone might enter on the form 1-800-555-1212 or they might enter their area code and do (619) 555-1212. Or just numbers and do 8585551212. Basically the 555-1212 combination is a fake, made up number and that's what I want this regex to do... to identify a fake number and simply give the error message. Is it possible? – Jaime May 01 '17 at 00:15
  • I don't know phone numbers can describe what makes a phone number invalid? Would `978-337-1555` be invalid? – chris85 May 01 '17 at 00:28
  • Take a look at https://meta.stackexchange.com/questions/142192/getting-email-notifications-when-comments-or-answers-are-posted for getting emails. – chris85 May 01 '17 at 00:28
  • 978-337-1555 would be a valid number... isn't there a way to tell it that there should be 4 or 5 characters after the "555"? – Jaime May 01 '17 at 00:52
  • Yes, but with an extension that wouldn't be correct. e.g. `(619) 555-1212 ext 1426` should be invalid still, right? Reading a bit more it seems some brazilian phones have `555` in them and are valid. – chris85 May 01 '17 at 00:55
  • @Jaime Hope my post will help you out... – Sahil Gulati May 01 '17 at 03:21
  • @chris85 Normally if someone uses a fake number, like 555-1212, they don't enter an extension of any kind. just fyi. – Jaime May 01 '17 at 08:08
  • Possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – mickmackusa May 16 '17 at 15:15

1 Answers1

0

Here we are using preg_replace to remove non-digit characters etc. to get a valid phone no. containing only digits.

Try this code snippet here

<?php

ini_set('display_errors', 1);

$phoneNo="(619) 555-1212";
$phoneNo=  preg_replace("/[^\d]/", "", $phoneNo);
if(strpos($phoneNo, "5551212")!==false)
{
    $error.= "<li><font face=arial>Phone number is <em><strong>required</strong></em> if you're truly serious about generating $500/Day.  Enter full number with area code.  Also, you should use only the following characters: '+' , '0-9' , ' ( ' , ' ) ' , 'ext' , or 'x'.  Example, <strong>(123) 555-1212</strong></font><br><br>"; 
    echo $errors=1;
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • @chris85 but in its question that person has given two inputs `"555-1212" or "5551212"` – Sahil Gulati May 01 '17 at 03:49
  • @chris85 i read the comments of OP, i think this solution can be okay? – Sahil Gulati May 01 '17 at 04:11
  • The update would be closer (although the `555` can be first or middle and the `1212` is just a placeholder). The question is unanswerable still to me though (there are numerous unaccounted for cases) and the sporadic OP responses aren't helping. – chris85 May 01 '17 at 04:16
  • @SahilGulati - Interesting. with this code, if the person enters a real phone number with an "ext. 123", will it work and go through as a legitimate number? Also, they might enter just "5551212" or they may enter an area code like "858-555-1212". These 2 are fake and the system should be able to identify and block it showing the error. Will this work that way? Also, the way it currently is, I like it because it only allows up to a certain number of character, like 10 digits for example. this new code seems to allow the user to enter a long string of digits. Know what I mean? :-) – Jaime May 01 '17 at 08:15
  • @chris85 - "and the sporadic OP responses aren't helping." Gee... so sorry I have a life. With the hectic day I had getting everything ready for a week-long vacation to New York and add to that the fact that Stack overflow does NOT email you of replies to a tread, how can I be expected to know when you're posting here? Which reminds me, my next post may not be until Monday the 8th, when I get back from New York. Hope you have a nice week. :-) – Jaime May 01 '17 at 08:19
  • @Jaime My code check whether a phone no. contains this `5551212` or not. It removes all characters and leaves the phone no. at the end. – Sahil Gulati May 01 '17 at 08:35
  • @Jaime in my code i have no check for length of phone no. If you want you can check it with `strlen($phoneNo)` – Sahil Gulati May 01 '17 at 08:43
  • @Jaime Currently you are missing numerous edge cases. What happens with the following numbers `1-213-555-1222`, `+22-333-555-1231` , `+55-512-1232`, `1-915-552-4123`, etc. I showed you how to set up the account so you get emails. Thanks for point out that you have a life, I guess we are wasting our time trying to help you? – chris85 May 01 '17 at 12:41
  • @chris85 - Ok... I'm back from New York... what an amazing vacation! I have to get some sleep now, but before I do, I just wanted to clarify something. When I commented that I have a life, I meant that I have a life outside of programming. Many of the amazing people that contribute here, pretty much dedicate every waking hour to writing code and helping people. They truly eat, breathe, and live it. I can't say the same about me. I don't have the skill level that people here have and I highly value and respect those that do. So thank you for your kind assistance in this. ;-) – Jaime May 08 '17 at 07:44