10

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.

tk_
  • 16,415
  • 8
  • 80
  • 90
Sami
  • 3,956
  • 9
  • 37
  • 52
  • The regex is `^\+?\d+$`. In HTML `pattern` attribute, it looks like `pattern="\+?\d+"`. If you need to limit the number of digits to a specific amount replace `\d+` with `\d{n}` where `n` is the exact number of digits you want. – Wiktor Stribiżew Jul 28 '23 at 11:29

6 Answers6

16

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

brandonscript
  • 68,675
  • 32
  • 163
  • 220
9

How about this?

^[\+\d]?(?:[\d-.\s()]*)$

works fine with bellow test cases:

test-string

You can test it here : https://regex101.com/r/mS9gD7/39

tk_
  • 16,415
  • 8
  • 80
  • 90
8

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
Rahul Gupta
  • 9,775
  • 7
  • 56
  • 69
2
^\+[0-9]+$

Try the above regex. You can test here http://gskinner.com/RegExr/. Are you also looking to validate length?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Srb1313711
  • 2,017
  • 5
  • 24
  • 35
2

^[0-9]+[0-9]*$ try this its working

  • 1
    Thanks for your answer! Would be nice if you provided some explanation about how and why this works though. – Janis Jansen Dec 19 '19 at 07:02
  • There must be an optional `+` at the beginning and why 2 character class when only one is needed? – Toto Dec 19 '19 at 11:20
-2

You can try this ^+[1-9]{1}[0-9]{10}$

Henry
  • 1,010
  • 8
  • 10