0 users online. Create an account or sign in to join them.Users


The title pretty much says it;
How to change the 'number' regular expression on the text input field, so it allows whitespaces as well, to make the validation a bit more tolerant on phone numbers?


I'm still learning regex myself, but here's how I'd do it:

Assuming you don't want negative phone numbers or phone numbers with dots in them then you can probably go with something like /^[\s\d]+$/

  • / / regex qualifiers
  • ^ start of string
  • $ end of string
  • [] set of single characters to match (multiple characters are "or"ed)
  • \s whitespace
  • \d digits
  • + one or more of the preceding character or set

However, this is super simple, it will match any set of digits with whitespace so

  1    2 0000000003456789    

is valid in this case. I'm sure someone will be along to provide a more rigid version at some point!

edit: note trailing and leading spaces

Thanks for the clear explanation, it's working perfectly!
I am gonna use this mini course as a basis for future use cases.


No problem.

Someone linked RegExr on here at some point, great for testing this sort of stuff. Also there's the wikipedia article on Regex and a fairly comprehensive .info site that I use for reference.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details