Pharo, Checking emails with regexp

In the previous post we build a login form and we saw that we should provide an user registration form.

We are going to build a registration form so we need to check emails. I will create a new class PnUtils to contain all the string-checking utilities that we need for our App:

<span class="nc">Object</span> <span class="nf">subclass:</span> <span class="ss">#PnUtils</span>
    <span class="nf">instanceVariableNames:</span> <span class="s">''</span>
    <span class="nf">classVariableNames:</span> <span class="s">''</span>
    <span class="nf">poolDictionaries:</span> <span class="s">''</span>
    <span class="nf">category:</span> <span class="s">'LeonardoBlog'</span>

Now, in a class method, I will implement my email-checking:

<span class="k">!</span><span class="nc">PnUtils class</span><span class="k"> methodsFor: </span><span class="s">'as yet unclassified'</span><span class="k">!</span>
<span class="nf">checkEmail:</span> <span class="nv">anEmail</span>
    <span class="o">^</span> <span class="nv">anEmail</span> <span class="nf">asUppercase</span> <span class="nf">matchesRegex:</span>  <span class="s">'[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z][A-Z][A-Z]?[A-Z]?'</span><span class="p">.</span>

As you see I check the email addresses using the RegExp feature of Smalltalk. The regexp is taken from this site.

I know that at this times my should send an activation email to let the user activate yourself but I won’t do it as I don’t want to mess up with SMTP configuration.

In this discussion I want to show you how simple is to use regexp from Pharo Smalltalk, without loading any external libraries.

Ok. We must test it to see if it’s working or not:

<span class="nc">TestCase</span> <span class="nf">subclass:</span> <span class="ss">#PnUtilsTest</span>
    <span class="nf">instanceVariableNames:</span> <span class="s">''</span>
    <span class="nf">classVariableNames:</span> <span class="s">''</span>
    <span class="nf">poolDictionaries:</span> <span class="s">''</span>
    <span class="nf">category:</span> <span class="s">'LeonardoBlog-Tests'</span>

<span class="nf">!</span><span class="nc">PnUtilsTest</span> <span class="nf">methodsFor:</span> <span class="s">'tests'</span><span class="nf">!</span>
<span class="nf">testEmailOk</span>
    <span class="bp">self</span> <span class="nf">assert:</span> (<span class="nc">PnUtils</span> <span class="nf">checkEmail:</span> <span class="s">'leonardoce@interfree.it'</span> )<span class="p">.</span>
    <span class="bp">self</span> <span class="nf">assert:</span> (<span class="nc">PnUtils</span> <span class="nf">checkEmail:</span> <span class="s">'leonardoce@interfree.com'</span> )<span class="p">.</span>

<span class="k">!</span><span class="nc">PnUtilsTest</span><span class="k"> methodsFor: </span><span class="s">'tests'</span><span class="k">!</span>
<span class="nf">testEmailNotOk</span>
    <span class="bp">self</span> <span class="nf">assert:</span> (<span class="nc">PnUtils</span> <span class="nf">checkEmail:</span> <span class="s">'leonardoce@interfree.sirtr'</span> ) <span class="nf">not</span><span class="p">.</span>

The tests should all be working.