Since phone numbers must conform to a pattern, you can use regular expressions to match the entered phone number against the pattern you define in regexp.
php has both ereg and preg_match() functions. I’d suggest using preg_match() as there’s more documentation for this style of regex.
An example
$phone = '000-0000-0000'; if(preg_match("/^[0-9]{3}-[0-9]{4}-[0-9]{4}$/", $phone)) { // $phone is valid }
Related Posts:
- How to validate an email address in PHP
- OR condition in Regex
- What is a Context Free Grammar?
- What does \’.- mean in a Regular Expression
- How can I exclude one word with grep?
- How can I validate an email address using a regular expression?
- Regular expression to match a line that doesn’t contain a word
- Matching a space in regex
- Difference between \b and \B in regex
- regex match any whitespace
- Regex: ignore case sensitivity
- What is the difference between the regular expressions [^\d\s] and [\D\S]
- grep –ignore-case –only
- Regex AND operator
- python re.split() to split by spaces, commas, and periods, but not in cases like 1,000 or 1.50
- Regular expression negative lookahead
- Regular Expression to match string starting with a specific word
- Regular Expressions: Is there an AND operator?
- Writing a very simple lexical analyser in C++
- Regular expression to match standard 10 digit phone number
- Regular Expression.how to add optional character at end of regex
- Regular Expression with wildcards to match any character
- What is a non-capturing group in regular expressions?
- Regular expression to match standard 10 digit phone number
- Python string.replace regular expression
- how to replace quotation marks with \”
- How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops
- Python string.replace regular expression
- Grep and Python
- Regex – Does not contain certain Characters
- How to replace ” \ ” with ” \\ ” in java
- A regular expression to exclude a word/string
- Regex how to match an optional character
- How to validate an email address in JavaScript
- Using Bash regex match (=~) where regex includes quotes (” characters)
- Searching for UUIDs in text with regex
- Regular expression for exact match of a string
- Using the star sign in grep
- Regex optional group
- Regex for string contains?
- Regular Expression – Validate Gmail addresses
- Java; String replace (using regular expressions)?
- How can I remove punctuation from input text in Java?
- How can I output only captured groups with sed?
- How to negate specific word in regex?
- What is the grep equivalent in Python?
- Which regular expression operator means ‘Don’t’ match this character?
- Python re.split() vs split()
- How to exclude a specific string constant?
- How to replace a whole line with sed?
- Validating IPv4 addresses with regexp
- Validate phone number with JavaScript
- Can a URL contain a semicolon and still be valid?
- How is \\n and \\\n interpreted by the expanded regular expression?
- Regex to match only uppercase “words” with some exceptions
- Regular Expression to reformat a US phone number in Javascript
- Regex to match only letters
- Finding the complement of a DFA?
- RegEx to find two or more consecutive chars
- Regex to match an optional ‘+’ symbol followed by any number of digits
- Remove all special characters from a string
- Regular expressions in C: examples?
- How to ignore whitespace in a regular expression subject string?
- Regex: matching up to the first occurrence of a character
- Regex credit card number tests
- Regular expression – starting and ending with a character string
- How do I match any character across multiple lines in a regular expression?
- Regex that matches integers in between whitespace or start/end of string only
- Regular Expressions- Match Anything
- What is the difference between square brackets and parentheses in a regex?
- Regex for “AND NOT” operation
- Escaping Parentheses in Regex
- How to replace a string in an existing file in Perl
- What is the best regular expression to check if a string is a valid URL?
- Regex empty string or email
- Wrap element with in any number of elements with PHP
- extract shortcodes from string
- sql query to put quotes around numbers in img tag
- Wrap First Character after in a tag
- preg_replace and comment_form_defaults
- Applying A Category to Existing Posts Where Page Title Matches Regex
- Preg Match All doesn’t Allow echo first character for WordPress Taxonomy Tags
- Extend PHP regex to cover “srcset” and “style” attributes
- How to set up redirects for category paginated pages after adding /category/ to permalink
- Adding regex filter to feedwordpress
- Get URL from shortcode tag
- get_users meta_query: REGEXP not working for matching new lines
- Adding span tags to post titles using regex
- Preg_Match(): compilation failed unknown property name
- preg_replace regex problem
- How to add no follow to specific links?
- Extract links from content
- Testing a Form’s Zipcode Field with Regular Expressions to Determine WordPress Page Behaviour
- Restrict comments with regular expression
- RegExp to EXCLUDE strip HTML (FOR SOMME) comments
- Replace Dash (-) and Underscore ( _ ) with Space
- How to strip/remove all blank spaces at the beginning/end of a search string
- Regex For Extracting First 4 letters of search term
- Is there a way to get wp_editor (tinymce) content?
- How to cut a section of a URL from a string with a regular expression?