0

is any way to validate phone number in two forms? I would like to validate numbers beginning with "+" and phone without "+". For example, it should pass numbers +238472636 and 193785626.

Pattern: /^[\+]\d+$/

2 Answers2

1

Use ? for optional characters or blocks:

Pattern: /^[\+]?\d+$/
Skarllot
  • 745
  • 6
  • 16
1

The ? quantifier matches the preceding element zero or one time. It is equivalent to {0,1}. ? is a greedy quantifier whose lazy equivalent is ??

so your reg. ex would be like this..

/^[\+]?\[0-9]+$/ OR
/^[\+]?\d+$/
Kandarp
  • 282
  • 1
  • 10