I want to validate a phone number this format +905555555555.
How can I write a regex expression to test for this?
I want to validate a phone number this format +905555555555.
How can I write a regex expression to test for this?
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}
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}$
<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:
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
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);
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)