How to match multiple phone number format in regex - php

How do I match multiple phone number length and format in regex?
^(?:080|070|081|\+?234)(?!.*-.*-)(?:\d(?:-)?){(8|10)}$
This is my code:
if (!preg_match("^(?:080|070|081|\+?234)(?!.*-.*-)(?:\d(?:-)?){(8)}$", $mobile)) {
// valid mobile number
}
I want to support the following number formats:
+2347036756899
+234-7036-756899
07036756899
08036756899
https://regex101.com/r/07TLhR/1

Related

PHP Regex for Greek mobile phone with prefix 69

I would wish a PHP regex in order to validate Greek mobile phone numbers.
The number must contains 10 digits.
The first two numbers must be 69xxxxxxxx
GREEK Mobile Number Validation
You can preg_match() to validate 10-digit mobile numbers ^69 first two number should be start with 69:
//Example
$mobile_number = 6955555559;// Your number
//Checking phone number
if(preg_match('/^69[0-9]{8}+$/', $mobile_number)){
echo "Correct number";
}else{
echo "Wrong number";
}
//Following script is PHP Regex for Greek mobile phone with prefix 69 and to validate 10-digit numbers. This ^[6]{1}[9]{1} will check for particular 69 in front and this [0-9]{8}$ will check 8 numbers
$phone = "6988888888";
$validation = "/^[6]{1}[9]{1}[0-9]{8}$/";
preg_match($validation,$phone,$m);
echo $m[0];
// Output: 6988888888

Need a PHP Regex for validation of mobile numbers and address fields

I need a regular expression for validation the mobile number and address field, but input is from PSV file (pipe-sign separated values) i.e.
XXXXXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX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XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXX|XXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXX|XXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXX|XXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXX||
this input is converted in this format:
86082522|801213075672|NRIC|013333344444|SYED ZEESHAN||0107777777|0000000000|||||NO 3046|ROAD 12 MK.7$14000$,HEALE,PNG$PNG$||SHAID ABAD PAKPATAN|KALA|URB5467||8827|20151029|29/10/2015|28/12/2015|113.1000||BHALWAPUR RAWAPALNI KKOTA BANU|||
now this input is okay and valid but if some user forgot to add the mobile address field and address field than it should be not allowed to save in database, now im going to example of invalid mobile number field
86082522|801213075672|NRIC|013333344444|SYED ZEESHAN||8##-777777|0000000000|||||NO 3046|ROAD 12 MK.7$14000$,HEALE,PNG$PNG$||SHAID ABAD PAKPATAN|KALA|URB5467||8827|20151029|29/10/2015|28/12/2015|113.1000||BHALWAPUR RAWAPALNI KKOTA BANU|||
now above example is invalid mobile number, because its containing the some charcters as well. so i need a regex that can remove these charcters.
Okay how about this.
$string = '86082522|801213075672|NRIC|013333344444|SYED ZEESHAN||0111123444443|0000000000|||||NO 3046|ROAD 12 MK.7$14000$,HEALE,PNG$PNG$||SHAID ABAD PAKPATAN|KALA|URB5467||8827|20151029|29/10/2015|28/12/2015|113.1000||BHALWAPUR RAWAPALNI KKOTA BANU|||';
$array = explode('|', $string);
if(preg_match('/^0\d{9,14}$/', $array[6])) {
echo 'true';
} else {
echo 'false';
}
Regex101 demo: https://regex101.com/r/iN5oT5/1
We require the string start with a 0 then have 9-14 ({9,14}) additional numbers (\d).

Regex for 10-13 digit (international) phone numbers

I need to deliver (international) phone numbers from a PHP registration form to an external party. The external party has the following rules for phone numbers.
10-13 digits
dashes are allowed
This means the following numbers are correct
0046701234567 (Swedish)
604-555-5555
5675555555
What would be the best (and most correct) regex to regulate and check all the phone numbers so I can store them in a global database to send them to the external party afterwards?
Just try with:
$input = '604-555-5555';
if (preg_match('/^\d{10,13}$/', str_replace('-', '', $input))) {
// valid
}
<?php
$input = '604-555-5555';
if (preg_match('/[\d-]{10,13}+/', $input, $r) ) {
// valid
foreach($r as $e){echo $e;}do formatting
}
?>

UK Mobile Phone Validation

I'm trying validate UK Mobile Phone Numbers, that are in the format:
075xxxxxxxx 077xxxxxxxx 078xxxxxxxx and 079xxxxxxxx
Each number must either start with the above 3 numbers and be 11 digits in length.
I've looked around and found several regex's such as:
RegEx1
RegEx2
But nothing I'm struggling to match my exact needs.
Has anyone got an example that would work with the numbers I'm trying to validate?
use this regex 07[5789]\d{8}
if you want to set dashes correct regex like this one 07[5789]\d{3}-\d{3}-\d{2}
07[57-9]\d{8}
07 -> exact match
[57-9] -> matches 5, 7, 8 and 9
\d{8} -> 8 digits more
Try this:
<?php
$telno = "07712345678";
preg_match("^07[5789]{1}[0-9]{8}^",$telno,$matches);
if(count($matches) > 0 && $matches[0] == $telno){
echo "valid tel no.";
}else{
echo "invalid no.";
}
?>

Validating UK phone numbers in PHP

I purchased a contact form. Great little thing but I need to convert the validation for the phone number to allow for UK number formats - in other words, to allow for spaces.
Right now it validates without spaces and has a minimum length of 8 characters:
if(is_numeric($phone))
{
if(!$phone || strlen($phone) < 8)
{
$error .= "Please enter your phone number without spaces.<br />";
}
}
else
{
$error .= "Please enter numeric characters in the phone number field.<br />";
}
Phone numbers are typically horrible for regex patterns, which is what you will need.
This pattern for example:
$pattern = "/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/";
$match = preg_match($pattern,$input);
if ($match != false) {
// We have a valid phone number
} else {
// We have an invalid phone number
}
That pattern will match with +44 included or not e.g.
all these will match:
07222 555555
(07222) 555555
+44 7222 555 555
These won't
7222 555555
+44 07222 555555
(+447222) 555555
There are a load of sites that offer tutorials / cheat sheets etc. for regular expressions try some of these:
http://regexlib.com/Default.aspx
as well as a very good stack overflow post:
A comprehensive regex for phone number validation
So you just want to allow spaces?
Then you could use str_replace() to ignore spaces, right at the beginning:
$phone = str_replace(' ', '', $phone);
The is_numeric function that you're using isn't really even a suitable choice for American phone numbers. For example it accepts hexadecimal numbers like 0xABCDEF, which it should reject.
For simple text matching like this, regular expressions are often the easiest solution. A regular expression specifies a pattern of text. PHP has functions to let you search for or replace regular expression matches in text.
If you define a phone number as a string of at least 7 characters containing only digits and spaces, the corresponding regular expression would be /^[0-9 ]{7,}$/. The text inside the brackets represents a set of characters, the {7,} indicates that we're looking for at least 7 of these characters in a row, and the ^ and $ indicate that our match should start at the beginning of the string and end at the end of the string. The PHP documentation has a section explaining regular expressions in greater detail.
You would use the preg_match function to
ensure the phone number matched:
if (preg_match('/^[0-9 ]{7,}$/', $phone)) {
This matches all UK formats with a wide variety of punctuation:
^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{5}\)?[\s-]?\d{4,5}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$
Extract the various parts using this pattern
^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)(44)\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)([1-9]\d{1,4}\)?[\s\d-]+)(?:((?:x|ext\.?\s?|\#)\d+)?)$
The country code is in $1 (and is null for national format). The NSN is in $2. The optional extension is in $3.
Remove all non-digits from $2 for further processing. The next step is to make sure the NSN is in a valid range and is a valid length (either 9 or 10 digits, depending on the range).
The list of patterns is too long to reproduce here but is available at:
http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers
You could parse the input to an integer, then validate for the correct number of digits;
$mobileNumber = intval($mobileNumber);
if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {
//number has the right number of digits for a UK mobile number
} else {
//number does not have the right number of digits
}
Explained;
$mobileNumber = intval($mobileNumber);
This removes all non numerical characters from the string, and leading zero (eg spaces, brackets, plus signs, decimals etc);
*if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {*
This regular expression then checks that the string contains either 12 or 10 digits which would cover 447712345678 (12 digits) or 7791234567 (10 digits). It is matched in two sub clauses (^\d{12}$) or (^\d{10}$) the carat (^) and dollar ($) represent to match everything from the very beginning (^) and to the very end ($) of the string. the \d represents any digit, and the following {12} means match the previous statement that number of times.
This does not mean that the number is valid, you could use an API like twillio to do additional validation of the numebr https://www.twilio.com/help/faq/sms/does-twilio-check-to-see-if-phone-numbers-can-receive-sms
For example;
$mobileNumber = '+447791234567';
$mobileNumber = intval($mobileNumber); //$mobileNumber is now 447791234567
if (preg_match('/(^\d{12}$)|(^\d{10}$)/',$mobileNumber)==TRUE) {
//$mobileNumber matches (^\d{12}$) and is valid
}
You could also add a check to ensure that number starts with either '07' or '447', required for UK mobiles, like this;
function isValidMobile($aNumber){
$aNumber = intval($aNumber);
return preg_match('/(^\d{12}$)|(^\d{10}$)/', $aNumber) && preg_match('/(^7)|(^447)/', $aNumber);
}
the intval removes leading zero,so the regular expression checks for a leadin 7 or 447.
I have found a solution with javascript according to the standards mentioned [here][1].**its not regex.**if you are using this solution there are three things to take care
validation can be used inside the UK
space cant be added. you need to type in your number without spaces
number has to begin with zero
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script >
var name=window.prompt("Enter cell number");
var sixformat=["013397","013398","013873","015242","015394","015395","015396","016973","016974","017683","017684","017687","019467","019755","019756"];
var twoformat=["01","02","03","05","07","08","09"];
sixdi=name.substring(0,6);
twodi=name.substring(0,2);
document.write("phone number:",name,"<br>");
document.write("phone-number length:",name.length);
if(sixformat.includes(sixdi)&&name.length==11)
{
document.write("<br><h1>valid</h1>");
}
else if(sixdi=="016977"&&name.length==10)
{
document.write("<br><h1>valid<h1>");
}
else if(twoformat.includes(twodi)&&(name.length==11))//011########
{
document.write("<br><h1>valid</h1>");
}
else if(twodi=="01"&&(name.length==10))
{
document.write("<br><h1>valid</h1>");
}
else if(name.substring(0,4)=="0800"&&(name.length==10))
{
document.write("<br><h1>Valid</h1></br>");
}
else
{
document.write("<h1>invalid</h1>");
}
</script>
</body>
</html>

Categories