Regular expression to match a certain number of digits anywhere in a string

I am trying to use the angular ng-pattern attribute to ensure that the text in the input box matches a specific regular expression. I do this to validate the form. The input that I use for this is the phone field. The telephone field can accept 10-digit phone numbers, but I do not want to require a specific format from my user, in addition, it must contain a 10-digit number. Thus, they will be valid.

  • 555-555-5555
  • (555) 555-5555

On the backend, I just delete all the formatting and save the raw numbers. In the past, I did this to remove all non-numeric characters, and then just make sure the length == 10. This is quite simple to do in a few lines of code, but can this regex handle something? This is not what I have ever tried to make a regular expression. I don’t want to support certain formats, because I don’t care if they accidentally enter additional space or if they want to enter their phone number as follows: 55-55-55-55-55. This does not matter to me, I just want the regular expression to match 10 digits and no more than somewhere in the string.

Thanks for helping the guys!

+4
source share
3

10 , ,

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

. demo (\D [^\d\n], ).

(\d\D*?){10} regex 10 .

+3
^(?!(?:.*?\d){11,})[^a-zA-Z\n]+$

. .

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

+1

You can create an attribute type directive for it and set the correctness with a dynamic regular expression.

0
source

All Articles