Reputation: 235
I need a regex for Number,Space and Special Character. Here are my examples:
+44 161 999 8888
011 44 (161) 999 8888
1-408-999 8888
+1 (408) 999 8888
222 8888
1-212-222 8888
+1 (212) 222 8888
+1 212 999 8888
001 (212) 999 8888
0161 999 8888
+44 (161) 999 8888
2222 8888
01-2222 8888
+44 1-2222 8888
+91-80-26584050
26584050
Upvotes: 0
Views: 164
Reputation: 2912
Try this:
^[0-9 ()\+\-]+$
The [] means the items in this set.
The + on the end means one or more of them.
The 0-9 means numbers in this range.
The space means a space character.
The ( and ) mean the parentheses characters
The + and - mean the plus and minus characters.
The ^ at the start and $ at the end say this is the complete statement from start to finish, with nothing else in between.
I think that covers your complete set
Upvotes: 2
Reputation: 3581
for example in php:
<?php
$input = "+44 161 999 8888";
//$input = "011 44 (161) 999 8888";
//$input = "1-408-999 8888";
//$input = "+1 (408) 999 8888";
//$input = "222 8888";
//$input = "0161 999 8888";
//$input = "01-2222 8888";
//$input = "+44 1-2222 8888";
//$input = "+91-80-26584050";
//$input = "26584050";
preg_match("/(^\+[0-9]+\ [0-9]+\ [0-9]+\ [0-9]+$|^[0-9]+\ [0-9]+\ \([0-9]+\)\ [0-9]+\ [0-9]+$|^[0-9]+\-[0-9]+\-[0-9]+\ [0-9]+$|^\+?[0-9]+\ \([0-9]+\)\ [0-9]+\ [0-9]+$|^[0-9]+\ [0-9]+$|^[0-9]+\ [0-9]+\ [0-9]+$|^[0-9]+\-[0-9]+\ [0-9]+$|^[0-9]+\ [0-9]+\-[0-9]+\ [0-9]+$|^\+[0-9]+\-[0-9]+\-[0-9]+$|^\+[0-9]+\ [0-9]+\-[0-9]+\ [0-9]+$|^[0-9]+$)/", $input, $m);
echo $input;
print_r($m);
regex separated:
/(
^\+[0-9]+\ [0-9]+\ [0-9]+\ [0-9]+$|
^[0-9]+\ [0-9]+\ \([0-9]+\)\ [0-9]+\ [0-9]+$|
^[0-9]+\-[0-9]+\-[0-9]+\ [0-9]+$|
^\+?[0-9]+\ \([0-9]+\)\ [0-9]+\ [0-9]+$|
^[0-9]+\ [0-9]+$|^[0-9]+\ [0-9]+\ [0-9]+$|
^[0-9]+\-[0-9]+\ [0-9]+$|^[0-9]+\ [0-9]+\-[0-9]+\ [0-9]+$|
^\+[0-9]+\-[0-9]+\-[0-9]+$|
^\+[0-9]+\ [0-9]+\-[0-9]+\ [0-9]+$|
^[0-9]+$)/
if you need specific lengh of the number, change "+" for {number} for example length of 4 chars {4}
[0-9]{4}
Upvotes: 1