Home > Back-end >  RegExp phone number with mandatory space
RegExp phone number with mandatory space

Time:05-31

Would love to know a regExp to validate a phone number. Rules:

  • It must start with
  • It must have one and only once space in it
  • It must have only numbers and no other special chars other than the space
 31 45847362 ->> ok
 1234 45847362 ->> ok
 1234 458473 62 ->> no
 3145847362 ->> no
045847362 ->> no
 31-45847362 ->> no
 (31) 45847362 ->> no
 (31)45847362 ->> no

I have this but the space is not mandatory, not sure why. I though every char was mandatory in regexp

/^[ ]*[{0,1}[0-9]{0,1}[\s./0-9]*$/;

CodePudding user response:

Would this (in PCRE syntax) plus a length check work?

^\ [0-9]*\s[0-9]*$

(then check for valid length of phone number plus a space)

  • Related