Regular Expressions 101

Community Patterns

Simple Phone Number validation

1

Regular Expression
PCRE2 (PHP >=7.3)

/
^(?![ -])(?!.*[- ]$)(?!.*[- ]{2})[0-9- ]+$
/
gm

Description

  • Default to 8 characters maxInput and minInput 14 characters

  • Regular Expression

    ^(?![ -])(?!.*[- ]$)(?!.*[- ]{2})[0-9- ]+$
    
  • Allows digits 0-9, and the single white spaces and single hyphens - characters, but restricts the spaces and hyphens at the start, end, and repetitions between the value.

  • Note the length of the phone number is not validated by the regex but by the maxInput and input constraints.

  • The error message for an invalid format, (to be translatable in Ar is supported).

    Please insert a valid contact number.

Example

  • Valid samples:
    • 0560000000
    • 000-000-0-0000
    • 056 000-000 00
  • Invalid samples:
    • 056--000--0000
    • 0A0-000-0-0000
    • 056 000 -000 00
Submitted by Sowed Castelli - 3 years ago (Last modified 3 years ago)