· 1 min read

Regex to check if there are at least x number of digits in a phone number string

This post gives you a regex so you can check if there are at least x numbers in a string. This is ideal for phone number fields where you don't care about the format but you do want there to be some numbers.

At work today I had an interesting problem to solve. I needed to validate a phone number field with a regex and without caring about the format of the phone number.

The client just wanted there to be at least 6 numbers in the phone number field. So on our stand up zoom call I asked for help and my boss came up with this regex which works a treat.

I've shared it here in case anyone else is looking for something similar.

^.*\d.*\d.*\d.*\d.*\d.*\d.*$

It's as simple as that. I was then able to use it in Umbraco Forms on phone number text fields.

If you want to increase the amount of numbers you are looking for, just add another *\d.

Comments and reactions