hawarden_
hawarden_

Reputation: 2170

regex : match telephone number with or without non-numerical character

Someone is spamming my site with fake telephone numbers, I want to filter all telephone numbers on my site. The numbers are in format :

0862123652

So ten numbers where the first one is always 0, but spammers also use :

08 62 1 2   3 ///6 \\\52
08 62 1 .....2 ***  3+++ ///6 \\\52

So I cannot filter these ones with parttern like 0[0-9]{9}. How could I do this?

Thanks.

Upvotes: 1

Views: 92

Answers (1)

Mariano
Mariano

Reputation: 6511

Check if the string has exactly 10 digits (starting with 0):

^\D*0\D*(?:\d\D*){9}$

If you need to narrow it down, you could change every \D with the specific set of symbols that may occur between digits. For example:

^[.*+/\\ ]*0[.*+/\\ ]*(?:\d[.*+/\\ ]*){9}$

I'm positive you could create these kind of expressions if you read a couple of minutes about regex syntax, so allow me to recommend:

  1. Regular Expressions Tutorial (regular-expressions.info). A quite comprehensive tutorial to learn regex.
  2. regex101.com. Allows you to test different expressions and understand the way a pattern matches the subjet string.

Upvotes: 2

Related Questions