-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
  • 539
  • 1
  • 9
  • 21

3 Answers3

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
  • 1,076
  • 1
  • 6
  • 17
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
  • 444
  • 4
  • 8
0
/^\+?\d+$/

Explanation:

\+? Optional "+" character.

\d+ Multiple numerical characters.

^ and $ matches entire strings (as opposed to searching within other strings)

Eric Wu
  • 908
  • 12
  • 35