snipt

Ctrl+h for KB shortcuts

PHP

validade e-mail address

<html>
<head><title>4-11 Validating e-mail addresses</title></head>
<style>
    .err { color : red ; font-weight : bold }
</style>
<body>
<form action="recipe4-11.php" method="post">
<input type="text" name="input" /><br/>
<input type="submit" value="Submit Form" /><br/><br/>
<?php
if ( $_SERVER['REQUEST_METHOD'] == "POST" )
{
    $input = $_POST['input'];
    if ( preg_match( "/^[-\w.][email protected]([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/", $input ) )
    {
        # Do some processing here - input if valid
    }
    else
    {
        print "<span class=\"err\">Bad e-mail address. Please correct ".
            "and resubmit the form</span><br/>";
    }
}
?>
</form>
</body>
</html>
https://snipt.net/embed/2b749870d3f451c3d5820ad26bf82eb5/
/raw/2b749870d3f451c3d5820ad26bf82eb5/
2b749870d3f451c3d5820ad26bf82eb5
php
PHP
28
2019-06-15T17:06:43
True
False
False
/api/public/snipt/5781/
validade-e-mail-address
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a> <a href="#L-19">19</a> <a href="#L-20">20</a> <a href="#L-21">21</a> <a href="#L-22">22</a> <a href="#L-23">23</a> <a href="#L-24">24</a> <a href="#L-25">25</a> <a href="#L-26">26</a> <a href="#L-27">27</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="x">&lt;html&gt;</span> </span><span id="L-2"><a name="L-2"></a><span class="x">&lt;head&gt;&lt;title&gt;4-11 Validating e-mail addresses&lt;/title&gt;&lt;/head&gt;</span> </span><span id="L-3"><a name="L-3"></a><span class="x">&lt;style&gt;</span> </span><span id="L-4"><a name="L-4"></a><span class="x"> .err { color : red ; font-weight : bold }</span> </span><span id="L-5"><a name="L-5"></a><span class="x">&lt;/style&gt;</span> </span><span id="L-6"><a name="L-6"></a><span class="x">&lt;body&gt;</span> </span><span id="L-7"><a name="L-7"></a><span class="x">&lt;form action=&quot;recipe4-11.php&quot; method=&quot;post&quot;&gt;</span> </span><span id="L-8"><a name="L-8"></a><span class="x">&lt;input type=&quot;text&quot; name=&quot;input&quot; /&gt;&lt;br/&gt;</span> </span><span id="L-9"><a name="L-9"></a><span class="x">&lt;input type=&quot;submit&quot; value=&quot;Submit Form&quot; /&gt;&lt;br/&gt;&lt;br/&gt;</span> </span><span id="L-10"><a name="L-10"></a><span class="cp">&lt;?php</span> </span><span id="L-11"><a name="L-11"></a><span class="k">if</span> <span class="p">(</span> <span class="nv">$_SERVER</span><span class="p">[</span><span class="s1">&#39;REQUEST_METHOD&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s2">&quot;POST&quot;</span> <span class="p">)</span> </span><span id="L-12"><a name="L-12"></a><span class="p">{</span> </span><span id="L-13"><a name="L-13"></a> <span class="nv">$input</span> <span class="o">=</span> <span class="nv">$_POST</span><span class="p">[</span><span class="s1">&#39;input&#39;</span><span class="p">];</span> </span><span id="L-14"><a name="L-14"></a> <span class="k">if</span> <span class="p">(</span> <span class="nb">preg_match</span><span class="p">(</span> <span class="s2">&quot;/^[-\w.][email protected]([A-z0-9][-A-z0-9]+\.)+[A-z]{2,4}$/&quot;</span><span class="p">,</span> <span class="nv">$input</span> <span class="p">)</span> <span class="p">)</span> </span><span id="L-15"><a name="L-15"></a> <span class="p">{</span> </span><span id="L-16"><a name="L-16"></a> <span class="c1"># Do some processing here - input if valid</span> </span><span id="L-17"><a name="L-17"></a> <span class="p">}</span> </span><span id="L-18"><a name="L-18"></a> <span class="k">else</span> </span><span id="L-19"><a name="L-19"></a> <span class="p">{</span> </span><span id="L-20"><a name="L-20"></a> <span class="k">print</span> <span class="s2">&quot;&lt;span class=</span><span class="se">\&quot;</span><span class="s2">err</span><span class="se">\&quot;</span><span class="s2">&gt;Bad e-mail address. Please correct &quot;</span><span class="o">.</span> </span><span id="L-21"><a name="L-21"></a> <span class="s2">&quot;and resubmit the form&lt;/span&gt;&lt;br/&gt;&quot;</span><span class="p">;</span> </span><span id="L-22"><a name="L-22"></a> <span class="p">}</span> </span><span id="L-23"><a name="L-23"></a><span class="p">}</span> </span><span id="L-24"><a name="L-24"></a><span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-25"><a name="L-25"></a><span class="x">&lt;/form&gt;</span> </span><span id="L-26"><a name="L-26"></a><span class="x">&lt;/body&gt;</span> </span><span id="L-27"><a name="L-27"></a><span class="x">&lt;/html&gt;</span> </span></pre></div> </td></tr></table>
php