-3

My use case is to validate a String only consists of + and the numbers. I have tried with so many regexes but couldn't able to fix my issue.

I have used /^[0-9]+$/ regex but it is failed in the 2 nd and 3rd(Obviously it is because it is having A Character) input.

My need is,

if the input is 00000094777216903 - TRUE
if the input is +947777216903     - TRUE (+ is fine if it is in the beginning of the string)
if the input is +947777216903A    - FALSE
if the input is 0000777216903A    - FALSE

Can anyone help me to achieve my need in JS.

Hariprasath
  • 509
  • 1
  • 9
  • 20

2 Answers2

1

Try this regex https://regex101.com/r/6wW84L/2

/(\+){0,1}[0-9]+$/

You have a working example below This /(+){0,1}[0-9]+$/ not //(+){0,1}[0-9]+$//

enter image description here

WiatroBosy
  • 768
  • 3
  • 8
1

I believe this will do the trick:

/\+?[0-9]+/

Let me know if this does not, maybe I can adjust my answer.

Also, maybe this site will help you to find the exact RegEx you are looking for: https://www.regextester.com. I sure find it to be a handy tool!

Leonardum
  • 286
  • 2
  • 6