score:1

Accepted answer

I think you need something really simple like /^\+\d* ?\d*$/ which means:

  • ^\+ starts with '+'
  • \d* then zero or more digits
  • ? (space?) then zero or one space
  • \d* then zero or more digits
  • $ end of string

E.g.

function checkValue(el) {
  let re = /^\+\d* ?\d*$/;
  document.getElementById('err').textContent = re.test(el.value)? 'good':'bad';
}
<input oninput="checkValue(this)"><br>
<span id="err"></span>

score:0

Try out this regex , And also check the regex in this LINK

const reg = /^\+\d+ \d+$/gm;

^+ :- will check that string is starting with +.
\d+ :- this will allow 1 or more digits
\s :- This will allow only one space .
$ :- this will ensure that string ends with digit and this will restrict the second use of space character .

score:0

My Regex is pretty bloated but it should work.

const regex = \^\+[\d]+(\s)*(\d)+$\g;
  1. ^+ just negates the power of +
  2. [\d]+ is letting you pick 0-9 digits 1 or more times
  3. (\s)* is a group and it allows for 0 or 1 space
  4. (\d)+$ can pick digits between 0-9 one or more time. And $ is just the end anchor

score:2

This should work:

^\+?\d+( \d+)?$
  • ^ - start anchor
  • \+? - optionally start with a plus sign
  • \d+ - require at least one digit to follow
  • ( \d+)? - Optionally allow a space which must be followed by one or more digits
  • $ - end anchor

https://regex101.com/r/pxbefb/1


Related Query

More Query from same tag