<?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; verification</title>
	<atom:link href="http://www.randomsnippets.com/tag/verification/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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>Knix</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 correct email format from a different site. 

Demo

Email:



Please type in your email again:






Here is [...]]]></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 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: #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: #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: #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: #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: #000066;">status</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</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: #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>

]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/04/01/how-to-verify-email-format-via-javascript/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Form verification via JavaScript</title>
		<link>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/</link>
		<comments>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/#comments</comments>
		<pubDate>Wed, 27 Feb 2008 07:27:12 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[demo]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[email form]]></category>
		<category><![CDATA[error warnings]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[form email]]></category>
		<category><![CDATA[onSubmit]]></category>
		<category><![CDATA[verification]]></category>
		<category><![CDATA[verifyForm]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/</guid>
		<description><![CDATA[
function verifyForm(form) {
    var userName = form.name.value;
    var userEmail = form.email.value;
    var success = 1;
    if (!userName) {
        document.getElementById("usernameMsg").style.display = "";
        form.name.style.backgroundColor = "yellow";
      [...]]]></description>
			<content:encoded><![CDATA[<p><script language="javascript">
function verifyForm(form) {
    var userName = form.name.value;
    var userEmail = form.email.value;
    var success = 1;
    if (!userName) {
        document.getElementById("usernameMsg").style.display = "";
        form.name.style.backgroundColor = "yellow";
        form.name.style.border = "3px red solid";
        success = 0;
    }
    else {
        form.name.style.backgroundColor = "";
        form.name.style.border = "";
        document.getElementById("usernameMsg").style.display = "none";
    }
    if (!userEmail) {
        document.getElementById("emailMsg").style.display = "block";
        form.email.style.backgroundColor = "yellow";
        form.email.style.border = "3px red solid";
        success = 0;
    }
    else {
        form.email.style.backgroundColor = "";
        form.email.style.border = "";
        document.getElementById("emailMsg").style.display = "none";
    }
    if(!success) {
        alert("The form is incomplete.  Please read the error message(s).");
        return false;
    }
    else {
        alert("The form was submitted succesfully!");
        return true;
    }
}
</script><br />
Are you looking for a simple way to verify a form that you have?  The example below demonstrates some of the common techniques used in verifying a form.  Test out the form by leaving at least one of the fields blank before you submit the form.  </p>
<fieldset>
<legend>Demo</legend>
<form method="POST" onSubmit="return verifyForm(this);">
Username<br />
<input type="text" name="name">
<div id="usernameMsg" style="display:none;"><span style="color:red;">Please fill in your username.</span></div>
<p>
Email<br />
<input type="email" name="email">
<div id="emailMsg" style="display:none;"><span style="color:red;">Please fill in your email.</span></div>
<p></p>
<input type="submit" value="Submit">
</form>
</fieldset>
<p><span id="more-8"></span><br />
Here is the plain HTML for the form:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;form method=&quot;POST&quot; onSubmit=&quot;return verifyForm(this);&quot;&gt;
Username&lt;input type=&quot;text&quot; name=&quot;name&quot;&gt;&lt;div id=&quot;usernameMsg&quot; style=&quot;display:none;&quot;&gt;&lt;span style=&quot;color:red;&quot;&gt;Please fill in your username.&lt;/span&gt;&lt;/div&gt;
&lt;br&gt;
Email&lt;input type=&quot;email&quot; name=&quot;email&quot;&gt;&lt;div id=&quot;emailMsg&quot; style=&quot;display:none;&quot;&gt;&lt;span style=&quot;color:red;&quot;&gt;Please fill in your email.&lt;/span&gt;&lt;/div&gt;
&lt;br&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;</pre></div></div>

<p>Notice that the error warnings such as the <b>Please fill in your username.</b> or <b>Please fill in your email.</b> have already been preloaded but hidden from the viewer by setting the <code>display</code> attribute to <code>none</code>.  If the user submits the form with one or more of the fields blank, 4 things will happen to notify the user (Note: I tried to squeeze in as many warning events as possible just for demonstration purposes so I apologize if it is a bit over the top):</p>
<ol>
<li>the background of the missing input field will turn yellow</li>
<li>a red border will appear around the missing input field</li>
<li>the hidden error message will become visible</li>
<li>an alert will pop up notifying the user that 1 or more fields are missing</li>
</ol>
<p>When the user clicks on the <b>Submit</b> button, the <b>onSubmit</b> event gets triggered and executes the <b>verifyForm()</b> JavaScript function.  If the <b>onSubmit</b> event returns a <code>false</code> value, the form does not get submitted.  This value is determined by the <b>verifyForm()</b> function, which returns <code>true</code> if all fields are filled in and returns <code>false</code> if one or more fields are missing.   </p>
<p>Here is a peek at the JavaScript <b>verifyForm()</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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> verifyForm<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> userName <span style="color: #339933;">=</span> form.<span style="color: #000066;">name</span>.<span style="color: #660066;">value</span>;
    <span style="color: #003366; font-weight: bold;">var</span> userEmail <span style="color: #339933;">=</span> form.<span style="color: #660066;">email</span>.<span style="color: #660066;">value</span>;
    <span style="color: #003366; font-weight: bold;">var</span> success <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span>;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>userName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;usernameMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span>;
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;yellow&quot;</span>;
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3px red solid&quot;</span>;
        success <span style="color: #339933;">=</span> 0;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
        form.<span style="color: #000066;">name</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;usernameMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>userEmail<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;emailMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;block&quot;</span>;
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;yellow&quot;</span>;
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;3px red solid&quot;</span>;
        success <span style="color: #339933;">=</span> 0;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">backgroundColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
        form.<span style="color: #660066;">email</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">border</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span>;
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;emailMsg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;none&quot;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>success<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;The form is incomplete.  Please read the error message(s).&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</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;The form was submitted succesfully!&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Lines 5-15 checks to see if the username field is blank or not.  If the field is blank, it uses the DOM to access the div that is holding our warning message to the user and sets the <code>display</code> attribute to <code>block</code> (line 6).  This makes the message visible to the user.  </p>
<p>Next, it accesses the input text field and dynamically changes the <code>backgroundColor</code> attribute to <code>yellow</code> (line 7). </p>
<p>Then, it accesses the input text field again and dynamically changes the <code>border</code> attribute to <code>3px red solid</code> (line 8).  Here is a quick breakdown of the attribute we just used:</p>
<ul>
<li>3px &#8211; the width of the border</li>
<li>red &#8211; the color of the border</li>
<li>solid &#8211; the style of the border</li>
</ul>
<p>Finally, it sets the <code>success</code> variable to <code>0</code> which is equivalent to a <code>false</code> value (line 9).  We will use this variable later on to decide if the form is ready to be submitted or not.</p>
<p>The <code>else</code> statement in lines 11-14 just resets all the styling back to the default values.  This is for the case where a user is resubmitting the form so we want to remove all the warnings just in case they were activated from any previous submissions.</p>
<p>Lines 16-26 does the same exact thing except it is for the email text input field.  Remember, all we are doing in this example is checking to see if the field is empty or not.  This is to keep the code simple.</p>
<p>Line 27 checks the <code>success</code> variable to see if it had been set to false.  If so, an alert is executed telling the user that one or more fields were missing and to check the error messages (line 28).  Then it returns a <code>false</code> value to prevent the form from being submitted (line 29).</p>
<p>If the form has been filled-in completely, the <code>else</code> block gets executed in lines 31-34.  In this case, an alert is executed saying that the form was submitted successfully.  As a resutl, the function returns the value <code>true</code> which then submits the form.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/02/27/form-verification-via-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
