<?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>Random Snippets &#187; checkEmail</title>
	<atom:link href="http://www.randomsnippets.com/tag/checkemail/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code for Web Developers</description>
	<lastBuildDate>Fri, 25 Nov 2011 23:28:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>How to validate email format via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 05:30:08 +0000</pubDate>
		<dc:creator>Allen Liu</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[checkEmail]]></category>
		<category><![CDATA[emailRegEx]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[javascript demo]]></category>
		<category><![CDATA[javascript email]]></category>
		<category><![CDATA[javascript function]]></category>
		<category><![CDATA[onclick event]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[valid email address]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[verifyEmail]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=13</guid>
		<description><![CDATA[This JavaScript demo verifies that an email address is in the correct format and that the user has typed in the same address in both fields to prevent mistyping of the address. I have borrowed the regex for verifying the &#8230; <a href="http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><script src="/wp-includes/js/verifyEmail.js" language="Javascript" type="text/javascript"></script><br />
This JavaScript demo verifies that an email address is in the correct format and that the user has typed in the same address in both fields to prevent mistyping of the address.  I have <a href="http://www.regular-expressions.info/email.html" target="_blank">borrowed the regex for verifying the correct email format</a> from a different site. </p>
<fieldset>
<legend>Demo</legend>
<form name="myform">
Email:<br />
<input type="text" name="email1">
<p>
<p>
Please type in your email again:<br />
<input type="text" name="email2">
<p>
<input type="button" onclick="verifyEmail();" value="Check Email Address">
</form>
</fieldset>
<p><span id="more-13"></span><br />
Here is the plain HTML of the form.  Please note that I have removed the action attribute because this is only a demo and we are only concerned with the client-side verification of the JavaScript function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myform&quot;&gt;
Email: &lt;input type=&quot;text&quot; name=&quot;email1&quot;&gt;
&lt;p&gt;&lt;p&gt;
Please type in your email again: &lt;input type=&quot;text&quot; name=&quot;email2&quot;&gt;
&lt;p&gt;&lt;p&gt;
&lt;input type=&quot;button&quot; onClick=&quot;verifyEmail();&quot; value=&quot;Check Email Address&quot;&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Nothing exciting here except in line 6 where the <b>onClick</b> event is triggered when the user clicks on the button.  This event will launch the <b>verifyEmail()</b> JavaScript function which will check the user input of the email address.</p>
<p>Here is the code for the <b>verifyEmail()</b> function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> verifyEmail<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>     
<span style="color: #003366; font-weight: bold;">var</span> emailRegEx <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email1</span>.<span style="color: #660066;">value</span>.<span style="color: #660066;">search</span><span style="color: #009900;">&#40;</span>emailRegEx<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please enter a valid email address.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email1</span>.<span style="color: #660066;">value</span> <span style="color: #339933;">!=</span> document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">email2</span>.<span style="color: #660066;">value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Email addresses do not match.  Please retype them to make sure they are the same.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Woohoo!  The email address is in the correct format and they are the same.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">status</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This function will return a <b>false</b> value if the user does not type in an email address in the correct format or if the user fails to type in the same email address in both fields.  If the email addresses passes both these tests, then the function will return a <b>true</b> value.</p>
<p>The <a href="http://www.regular-expressions.info/email.html" target="_blank">regex for validating the correct email format in line 2 has been borrowed</a> and edited slightly by adding the <b>i</b> flag so that the regex is case-insensitive.</p>
<p>The <b>if</b> block in lines 3-6 checks to see if the email address in the first field passes the regex check by invoking the <b>search()</b> method on our email string value.  This method accepts regex expressions as arguments to check the string and returns the position of the specified value in the string or a <b>-1</b> if no match was found.  If the <b>search()</b> method returns a <b>-1</b>, then an alert would tell the user that an invalid email address has been entered and returns a <b>false</b> value.</p>
<p>Lines 7-10 checks to see if the user has entered the same string in both email fields.  This is done by a simple equality check on the string values.  If the strings are different, an alert warns the user that the email addresses are not the same and returns a <b>false</b> value.</p>
<p>If we have passed the two tests, then <b>else</b> block in lines 11-14 gets executed.  An alert tells the user that he or she has done a good job and the function returns a <b>true</b> value so that you  can continue with the form submission.</p>
<p>Ideally, you would have this function run during the <b>onSubmit</b> attribute for the <b>form</b> tag.  This way, the form will get submitted when the <b>checkEmail()</b> function returns a <b>true</b> value.  Here is an example of how your form would look like:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form name=&quot;myForm&quot; method=&quot;POST&quot; action=&quot;myTargetFile&quot; onSubmit=&quot;return checkEmail()&quot;&gt;
Email: &lt;input type=&quot;text&quot; name=&quot;email1&quot;&gt;
&lt;p&gt;&lt;p&gt;
Please type in your email again: &lt;input type=&quot;text&quot; name=&quot;email2&quot;&gt;
&lt;p&gt;&lt;p&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>If you found that my code was helpful in any way, shape, or form and would like to buy me a <strong>beer</strong>, please use the <strong>Donate</strong> button below =)  Cheers!</p>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="P7VD7JXQGY2YJ">
<input type="image" style="border: none;" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/WEBSCR-640-20110306-1/en_US/i/scr/pixel.gif" width="1" height="1"><br />
</form>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

