<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jason Doucette&#039;s Business By Bootstraps &#187; Regular Expressions</title>
	<atom:link href="http://jasondoucette.ca/category/regular-expressions/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasondoucette.ca</link>
	<description>Daily thoughts from the trenches of a self-funded company</description>
	<lastBuildDate>Sat, 14 Jan 2012 23:22:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Regex: Make sure a phone number is 10 digits</title>
		<link>http://jasondoucette.ca/regex-make-sure-a-phone-number-is-10-digits/</link>
		<comments>http://jasondoucette.ca/regex-make-sure-a-phone-number-is-10-digits/#comments</comments>
		<pubDate>Sat, 09 Feb 2008 17:06:40 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Howto]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Validators]]></category>

		<guid isPermaLink="false">http://jasondoucette.ca/2008/02/09/regex-make-sure-a-phone-number-is-10-digits/</guid>
		<description><![CDATA[I needed a generic validator for a phone number, and I started thinking about the format of the number but at the end of the day I just want to be sure that there are 10 digits in the entry, and the user can decorate that however he or she like with brackets, hyphens, dots and spaces, but as long as there are 10 digits in the mix and there aren&#8217;t any clearly invalid characters like letters or weird punctuation, I&#8217;m good. So: a regex to make sure that a string contains at least 10 digits along with any optional [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I needed a generic validator for a phone number, and I started thinking about the format of the number but at the end of the day I just want to be sure that there are 10 digits in the entry, and the user can decorate that however he or she like with brackets, hyphens, dots and spaces, but as long as there are 10 digits in the mix and there aren&#8217;t any clearly invalid characters like letters or weird punctuation, I&#8217;m good.</p>
<p>So: a regex to make sure that a string contains at least 10 digits along with any optional spaces, dashes, brackets and dots:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<p class="csharpcode">&nbsp;</p>
<div class="csharpcode">
<pre><span class="lnum">   1:  </span>Regex tester = <span class="kwrd">new</span> Regex(<span class="str">@"^([-() ]*\d[-() .]*){10}$"</span>);</pre>
<pre><span class="lnum">   2:  </span></pre>
<pre><span class="lnum">   3:  </span>Assert.IsTrue(tester.IsMatch(<span class="str">"123-456-7890"</span>));</pre>
<pre><span class="lnum">   4:  </span>Assert.IsTrue(tester.IsMatch(<span class="str">"123 456 7890"</span>));</pre>
<pre><span class="lnum">   5:  </span>Assert.IsTrue(tester.IsMatch(<span class="str">"1234567890"</span>));</pre>
<pre><span class="lnum">   6:  </span>Assert.IsTrue(tester.IsMatch(<span class="str">"(123) 456-7890"</span>));</pre>
<pre><span class="lnum">   7:  </span>Assert.IsTrue(tester.IsMatch(<span class="str">"123.456.7890"</span>));</pre>
<pre><span class="lnum">   8:  </span>Assert.IsTrue(tester.IsMatch(<span class="str">"123 4567890"</span>));</pre>
<pre><span class="lnum">   9:  </span></pre>
<pre><span class="lnum">  10:  </span>Assert.IsFalse(tester.IsMatch(<span class="str">""</span>));</pre>
<pre><span class="lnum">  11:  </span>Assert.IsFalse(tester.IsMatch(<span class="str">"123"</span>));</pre>
<pre><span class="lnum">  12:  </span>Assert.IsFalse(tester.IsMatch(<span class="str">"werew"</span>));</pre>
<pre><span class="lnum">  13:  </span>Assert.IsFalse(tester.IsMatch(<span class="str">"12345678901"</span>));</pre>
<pre><span class="lnum">  14:  </span>Assert.IsFalse(tester.IsMatch(<span class="str">"123x567890"</span>));</pre>
<pre><span class="lnum">  15:  </span>Assert.IsFalse(tester.IsMatch(<span class="str">"123-456-7890 x32"</span>));</pre>
<pre><span class="lnum">  16:  </span>Assert.IsFalse(tester.IsMatch(<span class="str">"123-456-7890x"</span>));</pre>
</div>
<p>It&#8217;s up to the application to filter and reformat the entry for internal storage.  I like just storing the digits so later applications can reformat as needed, so I go with something like this:<br />
<!-- code formatted by http://manoli.net/csharpformat/ --></p>
<p class="csharpcode">&nbsp;</p>
<div class="csharpcode">
<pre><span class="lnum">   1:  </span><span class="kwrd">string</span> enteredNumber = <span class="str">"(123) 456-7890"</span>;</pre>
<pre><span class="lnum">   2:  </span><span class="kwrd">string</span> justDigits =</pre>
<pre><span class="lnum">   3:  </span>    <span class="kwrd">new</span> Regex(<span class="str">@"[^\d]"</span>).Replace(enteredNumber, <span class="str">""</span>);</pre>
<pre><span class="lnum">   4:  </span>Assert.AreEqual(<span class="str">"1234567890"</span>, justDigits);</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jasondoucette.ca/regex-make-sure-a-phone-number-is-10-digits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

