1

I want to validate a phone number this format +905555555555.

How can I write a regex expression to test for this?

redsquare
  • 77,122
  • 20
  • 149
  • 156
PsyGnosis
  • 1,429
  • 6
  • 21
  • 28

6 Answers6

2

one "+" and 12 numbers. hint: escape the "+".

^\+(90)\([2-5]{1}\)[0-9]{9}

or not starts with +;

\+(90)\([2-5]{1}\)[0-9]{9}
Mert Susur
  • 573
  • 7
  • 17
1

if you need it to start with a "+" and a "9" and 11 digits:

^[+]9\d{11}$

I recommend that you understand how regEx work, take a look at this usefull tester: http://www.sweeting.org/mark/html/revalid.php

At the bottom they explain what each operator means.

Also there are all sort of examples at the internet.

EDIT: after reading the OP's comments, in order for the number to start with "+90" and then have 10 digits, you can use the following expression:

^[+]90\d{10}$
jackJoe
  • 10,908
  • 8
  • 47
  • 63
1
<script type="text/javascript">

var test = "+905555555555";
var re = new RegExp(/^\+[0-9]{12}$/gi);

if(re.test(test))
{
alert("Number is valid");
} else {
alert("Number is not valid");
}
</script>

Check out this site for testing out your Regular Expressions: http://www.regextester.com/index2.html

And a good starting point to learn is here:

http://www.regular-expressions.info/

Sam Huggill
  • 3,068
  • 3
  • 27
  • 32
1

To cover your additional specifications use this:

^\+90\([2-5]\)\d{9}$

You definitely need the anchors ^ and $ to ensure that there is nothing ahead or after your string. e.g. if you don't use the $ the number can be longer as your specified 12 numbers.

You can see it online here on Regexr

stema
  • 85,585
  • 19
  • 101
  • 125
0

Adjusted a bit to answer question for a 12 digit regex.

// +905555555555 is +90 (555) 555-5555
let num = '+905555555555';
/^\+90\d{10}$/.test(num);
SoEzPz
  • 13,409
  • 8
  • 60
  • 61
0

If it's exactly that format then you could use something like: /^\+\d{12}\z/

If you'd like to allow some spaces/dashes/periods in it but still keep it at 12 numbers: /^\+(?:\d[ .-]?){12}\z/ Then you could remove those other chars with: s/[ .-]+//g (Perl/etc notation)

Qtax
  • 32,254
  • 9
  • 78
  • 117