Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phone number validation regular expression consist one plus sign in start and preceding digits

I am trying to validate phone number but cannot.

My requirement is phone number consist only of digits and + (plus symbol). The + can be only the first character.

For example: +123456489

I am using this regular expression but it is not working:

/^\+(?:[0-9]??)$/

Thanks in advance.

like image 669
Sami Avatar asked Jan 10 '14 16:01

Sami


2 Answers

I'd use this instead:

^\+?\d*$

Matches your + at the start, then any digit, dash, space, dot, or brackets.

See it in action: http://regex101.com/r/mS9gD7

like image 95
brandonscript Avatar answered Oct 09 '22 07:10

brandonscript


If you only want to allow + sign and that is only at the beginning of the number and does not want to allow any other characters or symbols other that the digits, then try the below regex:

var regEx = /^[+]?\d+$/;
regEx.test("+123345"); // this will be true
regEx.test("++123345"); // this will be false
regEx.test("1+23345"); // this will be false
regEx.test("111222"); // this will be true
like image 44
Rahul Gupta Avatar answered Oct 09 '22 05:10

Rahul Gupta