How can I use a regex to tell if a string has 10 digits?

I need to find a regex that tests that an input string contains exactly 10 numeric characters, while still allowing other characters in the string.

I'll be stripping all of the non-numeric characters in post processing, but I need the regex for client-side validation.

For example, these should all match:

  • 1234567890
  • 12-456879x54
  • 321225 -1234AAAA
  • xx1234567890

But these should not:

  • 123456789 (not enough digits)
  • 12345678901 (too many digits)

This seems like it should be very simple, but I just can't figure it out.

Answers


/^\D*(\d\D*){10}$/

Basically, match any number of non-digit characters, followed by a digit followed by any number of non-digit characters, exactly 10 times.


Need Your Help

How to change the text of a <label> which is hidden

javascript jquery html

I want to change the text of a label whose parent has its display hidden. I am not able to access the label because of its parents visibility.

Giving preferences to classpaths in java

java java-ee reflection

There are several classpath for a application (some of them are added during runtime). I want to read a file which present in two different locations. Depending upon the situation I have to load a

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.