Results 1 to 4 of 4

Thread: pregmatch script for phone number

  1. #1
    Join Date
    Sep 2017
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default pregmatch script for phone number

    I want to validate phone number with preg_match in this below format
    1 222 123 1234
    1 333 123 1234
    1231231234
    1-123-123-1234
    1123-123-1234
    123-123-1234

    My code
    preg_match("/^([0-9]){3}[0-9]{3}-[0-9]{4}$/", $field)

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I think that may be too many possibilities to cover in one preg_match test. However, assuming it would be OK if the field contained more spaces or dashes than expected, or a mixture of spaces and dashes, and perhaps not as expected, one could go with:

    PHP Code:
    preg_match("/^[0-9]{10,11}$/"preg_replace("/ |-/"''$field)) 
    You could test the string for its overall length first in order to make sure there weren't a lot of extra delimiters in there. You could use the strlen() function. It would have to return between 10 and 14 before stripping out the delimiters to have even a chance of fitting your template. That would narrow it down. Otherwise I think, if you wanted to be really precise in following your template, you would have to make a number of preg_match tests in a row, one for each of the possible configurations, with of course some combining where possible), and if the field passed any of those, it would be accepted.

    On second thought, this would be pretty good, but still accept some things you haven't precisely listed in your template strings:

    PHP Code:
    preg_match("/^\d?[- ]?\d{3}[- ]?\d{3}[- ]?\d{4}$/"$field
    BTW \d is the same as [0-9] and ? is the same as {0,1}
    Last edited by jscheuer1; 10-05-2017 at 03:41 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    This would be even better. I'm no longer sure that anything not fitting your template strings would pass this (but something might):

    PHP Code:
    $delimiter strpos($field" ") === 1" " "-";
    preg_match("/^\d?" $delimiter "?\d{3}" $delimiter "?\d{3}" $delimiter "?\d{4}$/"$field); 
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    pkrishna42 (10-10-2017)

  5. #4
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    512
    Thanks
    9
    Thanked 61 Times in 59 Posts

    Default

    This is similar to jscheuer expression
    Code:
    ^(\+1|001)?\(?([0-9]{3})\)?([ .-]?)([0-9]{3})([ .-]?)([0-9]{4})
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  6. The Following User Says Thank You to Deadweight For This Useful Post:

    pkrishna42 (10-10-2017)

Similar Threads

  1. Replies: 5
    Last Post: 04-10-2017, 05:24 AM
  2. adding phone number to header of site
    By powell in forum CSS
    Replies: 6
    Last Post: 11-30-2011, 08:59 PM
  3. Replies: 7
    Last Post: 04-21-2010, 08:03 PM
  4. Phone Number ( 0 ) validation
    By dchopda in forum JavaScript
    Replies: 3
    Last Post: 02-09-2010, 03:39 PM
  5. Phone Number Validation
    By dchopda in forum JavaScript
    Replies: 1
    Last Post: 01-27-2010, 06:46 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •