<?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>Design Chemical - jQuery, Wordpress, Tutorials &#38; Plugins&#187; Validation</title>
	<atom:link href="http://www.designchemical.com/blog/index.php/category/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designchemical.com/blog</link>
	<description>Your Business Catalyst</description>
	<lastBuildDate>Sun, 22 Jan 2012 12:55:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>jQuery Animated Error Messages &amp; Form Validation</title>
		<link>http://www.designchemical.com/blog/index.php/jquery/jquery-animated-error-messages-form-validation/</link>
		<comments>http://www.designchemical.com/blog/index.php/jquery/jquery-animated-error-messages-form-validation/#comments</comments>
		<pubDate>Thu, 21 Apr 2011 13:24:19 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.designchemical.com/blog/?p=1754</guid>
		<description><![CDATA[<a href="http://www.designchemical.com/blog/index.php/jquery/jquery-animated-error-messages-form-validation/"><img align="left" hspace="5" width="150" src="http://www.designchemical.com/blog/wp-content/plugins/thumbnail-for-excerpts/jquery_code.png" class="alignleft wp-post-image tfe" alt="" title="" /></a><p>In today&#8217;s tutorial we are going to use jQuery animation to create more interesting and engaging form validation errors for a user registration form.</p>
<p>Our jQuery form submit and validation code is based on the same one we created in our &#8220;Create Your Own jQuery AJAX Form Submit With Validation&#8221; tutorial in February. The additional items that we are going &#8230; <a href="http://www.designchemical.com/blog/index.php/jquery/jquery-animated-error-messages-form-validation/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<p>In today&#8217;s tutorial we are going to use jQuery animation to create more interesting and engaging form validation errors for a user registration form.</p>
<p>Our jQuery form submit and validation code is based on the same one we created in our &#8220;<a href="/blog/index.php/jquery/create-your-own-jquery-ajax-form-submit-with-validation/">Create Your Own jQuery AJAX Form Submit With Validation</a>&#8221; tutorial in February. The additional items that we are going to add are:</p>
<div class="section">
<ul class="content-list">
<li>An additional code snippet to check that the user&#8217;s password re-entry matches</li>
<li>jQuery code that will create &#038; dynamically position our various validation error messages</li>
<li>jQuery animation code that will show the relevant error</li>
<li>An additional step that will fade out the error message when the input element gains focus</li>
</ul>
</div>
<p><a href="/lab/jquery/demo/jquery_animation_form_validation_errors.htm" class="demo">View Demo</a><a href="/lab/jquery/demo/download/jquery-animation-form-validation-errors.zip" class="demo">Download Source Files</a></p>
<h3 class="clear">The Form HTML</h3>
<p>A standard form with the same format that we used in the earlier tutorial:</p>
<pre class="brush: plain; title: ; notranslate">
&lt;form id=&quot;form-sign-up&quot; class=&quot;styled&quot; action=&quot;&quot; method=&quot;post&quot;&gt;
    &lt;fieldset&gt;
        &lt;h3&gt;Register Now!&lt;/h3&gt;
        &lt;ol&gt;
            &lt;li class=&quot;form-row&quot;&gt;
                &lt;label&gt;Name:&lt;/label&gt;
                &lt;input name=&quot;name&quot; type=&quot;text&quot; class=&quot;text-input required&quot; /&gt;
            &lt;/li&gt;
            &lt;li class=&quot;form-row&quot;&gt;
                &lt;label&gt;Email:&lt;/label&gt;
                &lt;input name=&quot;email&quot; type=&quot;text&quot; class=&quot;text-input required email&quot; /&gt;
            &lt;/li&gt;
            &lt;li class=&quot;form-row&quot;&gt;
                &lt;label&gt;Password:&lt;/label&gt;
                &lt;input name=&quot;password&quot; type=&quot;password&quot; id=&quot;password-1&quot; class=&quot;text-input required password&quot; /&gt;
            &lt;/li&gt;
            &lt;li class=&quot;form-row&quot;&gt;
                &lt;label&gt;Repeat Password:&lt;/label&gt;
                &lt;input name=&quot;password1&quot; type=&quot;password&quot; id=&quot;password-2&quot; class=&quot;text-input required password&quot; /&gt;
            &lt;/li&gt;
            &lt;li class=&quot;button-row&quot;&gt;
                &lt;input type=&quot;image&quot; src=&quot;images/btn_sign_up_off.png&quot; alt=&quot;Sign Up&quot; value=&quot;OK&quot; class=&quot;btn-submit img-swap&quot; /&gt;
            &lt;/li&gt;
        &lt;/ol&gt;
    &lt;/fieldset&gt;
&lt;/form&gt;
</pre>
<p>The key items here are the classes we have assigned to the text elements, which will be used by our jQuery code to identify what validation rules and error messages should apply. We have 3 different types:</p>
<ul class="content-list">
<li>Name &#8211; has class &#8220;required&#8221;, which checks that the input is not empty and is used as the basis for all of our form elements</li>
<li>Email &#8211; has class &#8220;email&#8221;, which checks the input against a regular expression for matching standard email formats</li>
<li>Password/Re-enter Password &#8211; has class &#8220;password&#8221;, which will check the 2 entries against each other to make sure they match</li>
</ul>
<h3>Our Form CSS</h3>
<p>There are a couple of CSS rules, which are required in order for our error animation to work &#8211; these are highlighted in the code below with appropriate comments:</p>
<pre class="brush: plain; title: ; notranslate">
/*Form styles*/
.styled {
font: 15px Arial, sans-serif;
width: 422px;
margin: 20px auto;
background: url(images/bg_form.png) no-repeat 0 0;
padding-top: 20px;
}
.styled fieldset {
background: url(images/bg_form.png) no-repeat 0 100%;
padding: 0 25px 20px 25px;
position: relative;
}

/* Form rows */
.styled fieldset li.form-row {
margin-bottom: 5px;
padding: 3px 0;
clear: both;
position: relative; /* Required as error messages will be absolutely positioned */
}
.styled label {
display: block;
font-weight: bold;
float: left;
width: 100px;
line-height: 24px;
padding-top: 4px;
color: #555;
}
.styled fieldset li.button-row {
margin-bottom: 0;
padding: 5px 0 0;
text-align: right;
}

/* Text input styles */
/* Default */
.styled input.text-input {
height: 22px;
width: 254px;
padding: 5px 8px;
background: url(images/bg_input.png) no-repeat 0 0;
border: none;
font: normal 15px Arial, sans-serif;
color: #333;
line-height: 1em;
}
</pre>
<h3>The Form Validation CSS</h3>
<p>The following CSS rules are for styling and setting up the form error messages:</p>
<pre class="brush: plain; title: ; notranslate">

/* Form Validation */
/* CSS code to create the error messages */
.styled span.error {
font: bold 11px Arial, sans-serif;
line-height: 1em;
color:#fff;
text-shadow: 1px 1px 1px #000;
background: url(images/arrow_error.png) no-repeat 0 center;
border-right: 1px solid #6c0202;
height: 11px;
padding: 8px 15px 11px 20px;
display: none; /* Required as we will use jQuery to fade in the error message */
position: absolute; /* Required */
top: 3px;
right: 0; /* Required - initially set all error messages to the right of the form row */
}

/* Change the input field styles when an error message is present */
.styled fieldset li.error input.text-input {
background-position: 0 -64px;
}
</pre>
<div class="banner-horizontal">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-7740611438413740";
/* Banner Mid Post */
google_ad_slot = "8625321051";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<h3>The jQuery Code</h3>
<p>The code below includes comments explaining each function in more detail:</p>
<pre class="brush: jscript; title: ; notranslate">
// Form validation and submit when button is clicked
	$('.btn-submit').click(function(e){

		// Declare the function variables:
		// Parent form, form URL, email regex and the error HTML
		var $formId = $(this).parents('form');
		var formAction = $formId.attr('action');
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var $error = $('&lt;span class=&quot;error&quot;&gt;&lt;/span&gt;');

		// Prepare the form for validation - remove previous errors
		$('li',$formId).removeClass('error');
		$('span.error').remove();

		// Validate all inputs with the class &quot;required&quot;
		$('.required',$formId).each(function(){
			var inputVal = $(this).val();
			var $parentTag = $(this).parent();
			if(inputVal == ''){
				$parentTag.addClass('error').append($error.clone().text('Required Field'));
			}

			// Run the email validation using the regex for those input items also having class &quot;email&quot;
			if($(this).hasClass('email') == true){
				if(!emailReg.test(inputVal)){
					$parentTag.addClass('error').append($error.clone().text('Enter valid email'));
				}
			}

			// Check passwords match for inputs with class &quot;password&quot;
			if($(this).hasClass('password') == true){
				var password1 = $('#password-1').val();
				var password2 = $('#password-2').val();
				if(password1 != password2){
				$parentTag.addClass('error').append($error.clone().text('Passwords must match'));
				}
			}
		});

		// All validation complete - Check if any errors exist
		// If has errors
		if ($('span.error').length &gt; 0) {

			$('span.error').each(function(){

				// Set the distance for the error animation
				var distance = 5;

				// Get the error dimensions
				var width = $(this).outerWidth();

				// Calculate starting position
				var start = width + distance;

				// Set the initial CSS
				$(this).show().css({
					display: 'block',
					opacity: 0,
					right: -start+'px'
				})
				// Animate the error message
				.animate({
					right: -width+'px',
					opacity: 1
				}, 'slow');

			});
		} else {
			$formId.submit();
		}
		// Prevent form submission
			e.preventDefault();
	});

	// Fade out error message when input field gains focus
	$('.required').focus(function(){
		var $parent = $(this).parent();
		$parent.removeClass('error');
		$('span.error',$parent).fadeOut();
	});
</pre>
<p>The comments are fairly self explanatory. So to briefly summarise the jQuery code:</p>
<ol>
<li>When the submit button is clicked the validation process starts by declaring all function variables, including the error message span tag</li>
<li>Clean up the form by removing any errors span tags plus the error class on the form rows from the previous click so we are starting with a clean slate</li
<li>Loop through all input fields with class &#8220;required&#8221; and first check if they contain text</li>
<li>Validate input fields with class &#8220;email&#8221; using a standard regex pattern for email address formats</li>
<li>Get the 2 password field values and check if they match</li>
<li>If any of our checks returns an error indentify that form row by adding class=&#8221;error&#8221;, clone the error span tag, append to the form row and insert the relevant error message text</li>
<li>Position each error message span tag to the right of the form row based on the total width of the message</li>
<li>Fade in each error message and alert the user by animating 5px to the left</li>
<li>Fade out and remove the errors when the user clicks on the text input field</li>
</ol>
<p>With the basic process now in place you can easily modify and expand the animation functions and try different effects to suit your users and website.</p>
<p><a href="/lab/jquery/demo/jquery_animation_form_validation_errors.htm" class="demo">View Demo</a><a href="/lab/jquery/demo/download/jquery-animation-form-validation-errors.zip" class="demo">Download Source Files</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.designchemical.com/blog/index.php/jquery/jquery-animated-error-messages-form-validation/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Form Validation Using jQuery and Regular Expressions</title>
		<link>http://www.designchemical.com/blog/index.php/jquery/form-validation-using-jquery-and-regular-expressions/</link>
		<comments>http://www.designchemical.com/blog/index.php/jquery/form-validation-using-jquery-and-regular-expressions/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 02:00:09 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[Website Coding]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.designchemical.com/blog/?p=952</guid>
		<description><![CDATA[<a href="http://www.designchemical.com/blog/index.php/jquery/form-validation-using-jquery-and-regular-expressions/"><img align="left" hspace="5" width="120" height="120" src="http://www.designchemical.com/blog/wp-content/uploads/2010/05/thumb_jquery_code.png" class="alignleft tfe wp-post-image" alt="thumb_jquery_code" title="thumb_jquery_code" /></a><h4>Updated 18th April 2011</h4>
<p>Regular expressions offer an extremely flexible and powerful way of adding validation to your website forms. Combined with jQuery, it allows you to ensure that data sent to the server matches all of your requirements.</p>
<p>In this post I have included several example regular expressions that we have used in our web design projects for validating &#8230; <a href="http://www.designchemical.com/blog/index.php/jquery/form-validation-using-jquery-and-regular-expressions/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<h4>Updated 18th April 2011</h4>
<p>Regular expressions offer an extremely flexible and powerful way of adding validation to your website forms. Combined with jQuery, it allows you to ensure that data sent to the server matches all of your requirements.</p>
<p>In this post I have included several example regular expressions that we have used in our web design projects for validating form input.</p>
<p>For this tutorial we assume you know how to create the HTML form and add jQuery to your site. For samples you can refer to previous posts &#8211; <a href="http://www.designchemical.com/blog/index.php/jquery/check-passwords-using-jquery/">check passwords using jQuery</a>, <a href="http://www.designchemical.com/blog/index.php/jquery/email-validation-using-jquery/">email validation using jQuery</a> or view the demo form.</p>
<p class="clear-float"><a href="http://www.designchemical.com/lab/jquery/demo/jquery_regular_expression_validation_samples.htm" class="demo">View Demo</a></p>
<p class="clear">For each example we have created a css class, which can then be assigned to the relevant form element. If you are creating your own validation code/plugin there are obviously more efficient ways of creating a complete validation system but for the tutorial we have kept each regular expression sample separate and also use the &#8220;keyup&#8221; event.</p>
<h3>jQuery To Handle the Submit Button</h3>
<p>In order to use these validation functions in a form we need to add jQuery that will check for the presence of any span elements with the class &#8220;error&#8221;. We do this by using the length property. If the length is > 0 the form submit can be stopped and the user alerted:</p>
<pre class="brush: jscript; title: ; notranslate">
$('#btn-submit').click(function() {
  if($('span.error').length &amp;gt; 0){
    alert('Errors!');
    return false;
  } else {
    $('#btn-submit').after('&lt;span class=&quot;error&quot;&gt;Form Accepted.&lt;/span&gt;');
    return false;
  }
});
</pre>
<p>Now that we have this code to check for errors, we can add any of the examples below to the form.</p>
<div class="banner-horizontal">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-7740611438413740";
/* Banner Mid Post */
google_ad_slot = "8625321051";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<h3>Example 1 &#8211; Validates Numeric Characters Only</h3>
<p>Accepts only 0 &#8211; 9</p>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-numeric').keyup(function() {
    $('span.error-keyup-1').hide();
    var inputVal = $(this).val();
    var numericReg = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$/;
    if(!numericReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-1&quot;&gt;Numeric characters only.&lt;/span&gt;');
    }
});
</pre>
<h3>Example 2 &#8211; No Special Characters</h3>
<p>Allows only letters, numbers and spaces. All other characters will return an error.</p>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-characters').keyup(function() {
    $('span.error-keyup-2').remove();
    var inputVal = $(this).val();
    var characterReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
    if(!characterReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-2&quot;&gt;No special characters allowed.&lt;/span&gt;');
    }
});
</pre>
<h3>Example 3 &#8211; Maximum of 8 Characters</h3>
<p>Allows all characters up to a maximum of 8. Useful for passwords, etc. The value can easily be increased/descreased by changing the {0,8}</p>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-limit-8').keyup(function() {
    $('span.error-keyup-3').remove();
    var inputVal = $(this).val();
    var characterReg = /^([a-zA-Z0-9]{0,8})$/;
    if(!characterReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-3&quot;&gt;Maximum 8 characters.&lt;/span&gt;');
    }
});
</pre>
<h3>Example 4 &#8211; US Phone Number</h3>
<p>Allows numbers 2-9 for the first and second group of 3 followed by 0-9 for the last 4 with the groups separated by &#8220;-&#8221; e.g:</p>
<ul>
<li>234-234-1234 = OK</li>
<li>134-234-1234 = Error</li>
<li>234-134-1234 = Error</li>
</ul>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-phone').keyup(function() {
    $('span.error-keyup-4').remove();
    var inputVal = $(this).val();
    var characterReg = /^[2-9]\d{2}-\d{3}-\d{4}$/;
    if(!characterReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-4&quot;&gt;Format xxx-xxx-xxxx&lt;/span&gt;');
    }
});
</pre>
<h3>Example 5 &#8211; Validate Date Format</h3>
<p>Allows date format &#8211; mm/dd/yyyy &#8211; including &#8220;/&#8221;. All other combinations will return errors e.g:</p>
<ul>
<li>01/31/2001 = OK</li>
<li>31/01/2001 = Error</li>
<li>1/01/2001 = Error</li>
</ul>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-date').keyup(function() {
    $('span.error-keyup-5').remove();
    var inputVal = $(this).val();
    var dateReg = /^[0,1]?\d{1}\/(([0-2]?\d{1})|([3][0,1]{1}))\/(([1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))$/;
    if(!dateReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-5&quot;&gt;Invalid date format.&lt;/span&gt;');
    }
});
</pre>
<h3>Example 6 &#8211; Check For Possible Fake Text</h3>
<p>This is an interesting example, which checks for possible fake text being entered into your form. The regex looks for groups of the same letters occuring in groups of 3 or more e.g:</p>
<ul>
<li>foo = OK</li>
<li>fff = Error</li>
</ul>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-fake').keyup(function() {
    $('span.error-keyup-6').remove();
    var inputVal = $(this).val();
    var fakeReg = /(.)\1{2,}/;
    if(fakeReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-6&quot;&gt;Invalid text.&lt;/span&gt;');
    }
});
</pre>
<h3>Example 7 &#8211; Check Email Address Format</h3>
<p>This is a standard regular expression, which is used to validate email addresses to ensure they follow the standard format:</p>
<ul>
<li>email@email.com = OK</li>
<li>email.email.com = Error</li>
</ul>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-email').keyup(function() {
    $('span.error-keyup-7').remove();
    var inputVal = $(this).val();
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    if(!emailReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-7&quot;&gt;Invalid Email Format.&lt;/span&gt;');
    }
});
</pre>
<h3>Example 8 &#8211; No Free Email Addresses</h3>
<p>Another useful email validation regular expression checks for email addresses using free emails &#8211; in the example below we check for yahoo, gmail and hotmail:</p>
<ul>
<li>email@yahoo.com = Error</li>
<li>email@email.com = OK</li>
</ul>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-email-2').keyup(function() {
    $('span.error-keyup-8').remove();
    var inputVal = $(this).val();
    var emailFreeReg= /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)(?!aol.com)([\w-]+\.)+[\w-]{2,4})?$/;
    if(!emailFreeReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-8&quot;&gt;No Free Email Addresses.&lt;/span&gt;');
    }
});
</pre>
<h3>Example 9 &#8211; Visa Card Number Format</h3>
<p>For an example of validating a credit card we can use the following regular expression, which checks the input against the standard format for Visa credit cards &#8211; All card numbers must start with a 4 &#038; cards may either have 16 digits or 13 digits for older cards:</p>
<ul>
<li>4166000000000000 = OK</li>
<li>4166000000000 = OK</li>
<li>41660000000000001 = Error</li>
<li>2166000000000000 = Error</li>
</ul>
<pre class="brush: jscript; title: ; notranslate">
$('.keyup-cc').keyup(function() {
    $('span.error-keyup-9').remove();
    var inputVal = $(this).val();
    var ccReg = /^4[0-9]{12}(?:[0-9]{3})?$/;
    if(!ccReg.test(inputVal)) {
        $(this).after('&lt;span class=&quot;error error-keyup-9&quot;&gt;Invalid visa card number&lt;/span&gt;');
    }
});
</pre>
<p>Hopefully some of the above examples will be useful for your forms and also help demonstrate how powerful regular expressions can be.</p>
<p><a href="http://www.designchemical.com/lab/jquery/demo/jquery_regular_expression_validation_samples.htm" class="demo">View Demo</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.designchemical.com/blog/index.php/jquery/form-validation-using-jquery-and-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Create Your Own jQuery AJAX Form Submit With Validation</title>
		<link>http://www.designchemical.com/blog/index.php/jquery/create-your-own-jquery-ajax-form-submit-with-validation/</link>
		<comments>http://www.designchemical.com/blog/index.php/jquery/create-your-own-jquery-ajax-form-submit-with-validation/#comments</comments>
		<pubDate>Thu, 03 Feb 2011 23:42:04 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[Website Coding]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.designchemical.com/blog/?p=1149</guid>
		<description><![CDATA[<a href="http://www.designchemical.com/blog/index.php/jquery/create-your-own-jquery-ajax-form-submit-with-validation/"><img align="left" hspace="5" width="150" src="http://www.designchemical.com/blog/wp-content/plugins/thumbnail-for-excerpts/jquery_code.png" class="alignleft wp-post-image tfe" alt="" title="" /></a><p>For today&#8217;s tutorial we are going to do something a little more interesting and take several jQuery functions, which have been covered in our previous posts and combine them to create our own form submit process, which will be able to handle all of the forms in our projects.</p>
<p>This is a good exercise to see how various components/snippets can &#8230; <a href="http://www.designchemical.com/blog/index.php/jquery/create-your-own-jquery-ajax-form-submit-with-validation/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<p>For today&#8217;s tutorial we are going to do something a little more interesting and take several jQuery functions, which have been covered in our previous posts and combine them to create our own form submit process, which will be able to handle all of the forms in our projects.</p>
<p>This is a good exercise to see how various components/snippets can be combined to create a complete function.</p>
<p>The tutorial assumes you have the basic understanding of the HTML and CSS for the form.</p>
<p><a href="/lab/jquery/demo/jquery_ajax_form_submit_with_validation.htm" class="demo">View Demo</a><a href="http://www.designchemical.com/lab/jquery/demo/download/jquery_ajax_form_submit_with_validation.zip" class="demo">Download Source Files</a></p>
<h3 class="clear"><strong>The Basic Components</strong></h3>
<p>So, in order to create our process we are going to use the following components:</p>
<ol>
<li>A general HTML form format</li>
<li>Our standard CSS rules for styling the form</li>
<li>Add default text for our input fields, including a function to remove the default text when the form is submitted &#8211; see earlier blog post &#8211; <a href="http://www.designchemical.com/blog/index.php/jquery/add-default-text-to-form-fields-using-jquery/">Add Default Text To Form Fields Using jQuery</a></li>
<li>jQuery validation rules, which are applied when a form is submitted</li>
<li>jQuery code for processing the form and submitting the contents via AJAX</li>
</ol>
<h3><strong>1. HTML Form Format</strong></h3>
<p>Obviously everyone has their own preferences when it comes to how you code your form. After experimenting with several different formats I found that using an ordered list to handle the layout, which each form input field and associated label being one list item, has been easiest to use and so far has been robust enough to take most standard form requirements.</p>
<p>For the tutorial we will create a standard contact form with a name field, email, telephone and text area for comments:</p>
<pre class="brush: plain; title: ; notranslate">
&lt;form id=&quot;form-contact&quot; class=&quot;styled&quot; action=&quot;/user_functions.php&quot; method=&quot;post&quot;&gt;
		    &lt;fieldset&gt;
			  &lt;legend&gt;Contact Form&lt;/legend&gt;
			  &lt;ol&gt;
			    &lt;li class=&quot;form-row&quot;&gt;
				  &lt;label&gt;Email:&lt;/label&gt;
				  &lt;input id=&quot;input-email&quot; type=&quot;text&quot; class=&quot;text-input required email default&quot; name=&quot;email&quot; value=&quot;&quot; title=&quot;Enter Your Email Address&quot; /&gt;
				&lt;/li&gt;
				&lt;li class=&quot;form-row&quot;&gt;
				  &lt;label&gt;Name:&lt;/label&gt;
				  &lt;input id=&quot;input-name&quot; type=&quot;text&quot; class=&quot;text-input required default&quot; name=&quot;name&quot; value=&quot;&quot; title=&quot;Enter Your Full Name&quot; /&gt;
				&lt;/li&gt;
				&lt;li class=&quot;form-row&quot;&gt;
				  &lt;label&gt;Phone:&lt;/label&gt;
				  &lt;input id=&quot;input-phone&quot; type=&quot;text&quot; class=&quot;text-input&quot; name=&quot;phone&quot; value=&quot;&quot; /&gt;
				&lt;/li&gt;
				&lt;li class=&quot;form-row&quot;&gt;
				  &lt;label&gt;Comments:&lt;/label&gt;
				  &lt;textarea id=&quot;input-message&quot; class=&quot;text-area&quot; name=&quot;message&quot; cols=&quot;40&quot; rows=&quot;8&quot;&gt;&lt;/textarea&gt;
				&lt;/li&gt;
				&lt;li class=&quot;button-row text-right&quot;&gt;
				  &lt;input class=&quot;btn-submit&quot; type=&quot;submit&quot; value=&quot;submit&quot; name=&quot;submit&quot; /&gt;
				&lt;/li&gt;
			  &lt;/ol&gt;
			&lt;/fieldset&gt;
		  &lt;/form&gt;
</pre>
<p>As usual our jQuery will use css classes and IDs for the selectors. The key ones we need to include are:</p>
<ul class="content-list">
<li>Class &#8220;<strong>btn-submit</strong>&#8221; on the form submit button &#8211; used to trigger the function.</li>
<li>Class &#8220;<strong>required</strong>&#8221; on any input fields to be validated.</li>
<li>Class &#8220;<strong>email</strong>&#8221; as an additional class on those input fields to be tested against our regular expression for correctly formatted email addressed.</li>
</ul>
<h3><strong>2. The Form CSS</strong></h3>
<p>The CSS I use as the basis for this format is as follows:</p>
<pre class="brush: plain; title: ; notranslate">
.styled {
font-family: Arial, sans-serif;
}
.styled fieldset {
border: 1px solid #ccc; padding: 10px;
}
.styled fieldset legend {
font-size: 16px; font-weight: bold; color: #000; text-transform: capitalize; padding: 5px; background: #fff; display: block; margin-bottom: 0; border: 1px solid #ccc;
}
.styled fieldset ol, .styled fieldset ol li {
list-style: none;
}
.styled fieldset li.form-row {
margin-bottom: 3px; padding: 2px 0; width: 100%; overflow: hidden; position: relative;
}
.styled label {
font-size: 12px; display: block; font-weight: bold; float: left; width: 100px; margin-left: 5px; line-height: 24px;
}
.styled input.text-input, .styled .text-area {
background: #fefefe; border-top: 1px solid #909090; border-right: 1px solid #cecece; border-bottom: 1px solid #e1e1e1; border-left: 1px solid #bbb; padding: 3px; width: 220px; font-size: 12px;
}
.styled input.text-input.default.active, .styled .text-area.default.active {
color: #666; font-style: italic;
}
.styled fieldset li.button-row {
margin-bottom: 0; padding: 2px 5px;
}
form input.btn-submit {
padding: 3px 7px; border: 1px solid #fff; background: #066CAA; font-size: 12px;
}
</pre>
<p>This is pretty much the minimum styling we need to add to get the labels and input fields aligned and pad/border the form. This can then be applied to your forms by adding the class &#8220;styled&#8221;. You can build on the above CSS to make your forms a little more exciting!</p>
<p>Any specific rules you may want to add for each form can be done using it&#8217;s id. For our contact form #form-contact, we want to set the width to 320px and center on the page.</p>
<h3><strong>Validation CSS</strong></h3>
<p>In addition to the form layout we also need a few additional CSS rules, which will handle the styling of the validation results:</p>
<pre class="brush: plain; title: ; notranslate">
.styled span.error {
font-size: 11px; position: absolute; top: 0; right: 0; display: block; padding: 2px;
}
.styled fieldset li.error {
color: #D8000C; background: #fff0f0 url(../media/images/checkers.png) repeat; border: 1px solid #f9c7c7; padding: 5px 0;
}
.styled fieldset li.error label {
text-align: left;
}
</pre>
<h3><strong>3. The jQuery Code &#8211; Default Text Function</strong></h3>
<p>Before we tackle the form submission and validation lets add the jQuery code to handle the default text for the input fields &#8211; <a href="http://www.designchemical.com/blog/index.php/jquery/add-default-text-to-form-fields-using-jquery/">See earlier post for explanation</a>.</p>
<pre class="brush: jscript; title: ; notranslate">
$(&quot;.default&quot;).each(function(){
  var defaultVal = $(this).attr('title');
  $(this).focus(function(){
    if ($(this).val() == defaultVal){
      $(this).removeClass('active').val('');
    }
  });
  $(this).blur(function() {
    if ($(this).val() == ''){
      $(this).addClass('active').val(defaultVal);
    }
  })
  .blur().addClass('active');
});
</pre>
<p>We can now add default text to any of our form fields by simply adding the class &#8220;default&#8221; and using the title attribute to hold the default text. In our contact form example we have decided to add default text to both the email fields and the name field, which are both required.</p>
<p>Since we dont want the default text to be submitted with the form for any empty fields we add a jQuery function, which will automatically check all input fields with class &#8220;default&#8221; and remove the text if the value is the same as the title attribute:</p>
<pre class="brush: jscript; title: ; notranslate">
function defaulttextRemove(){
  $('.default').each(function(){
    var defaultVal = $(this).attr('title');
    if ($(this).val() == defaultVal){
      $(this).val('');
    }
  });
}
</pre>
<p>Now for our main function, handling the form submit. There are a few parts to this process:</p>
<ol>
<li>Check the form details &#8211; i.e. exactly which submit button has been pressed and the action URL for the data, since we are creating a generic function, which can be used by any forms, including multiple instances on one page.</li>
<li>Run the relevant validation on all required input fields &#8211; for our example we have both standard required text field and email validation using regular expressions to check for a correct email format.</li>
<li>Assuming no errors, we serialize the form data and using AJAX, post the contents to the form URL and return a result.</li>
</ol>
<div class="banner-horizontal">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-7740611438413740";
/* Banner Mid Post */
google_ad_slot = "8625321051";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<h3><strong>4. Form Validation and AJAX Form Submit jQuery Code</strong></h3>
<pre class="brush: jscript; title: ; notranslate">
// Declare the loading gif as a variable, which will be used later
var $loading = $('&lt;div class=&quot;loading&quot;&gt;&lt;img src=&quot;/media/images/loading.gif&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;');

// Form validation and submit when button is clicked
$('.btn-submit').click(function(e){

  // Declare the function variables - parent form, form URL and the regex for checking the email
     var $formId = $(this).parents('form');
     var formAction = $formId.attr('action');
     var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

  // In preparation for validating the form - Remove any active default text and previous errors
      defaulttextRemove();
      $('li',$formId).removeClass('error');
      $('span.error').remove();

  // Start validation by selecting all inputs with the class &quot;required&quot;
      $('.required',$formId).each(function(){
          var inputVal = $(this).val();
          var $parentTag = $(this).parent();
          if(inputVal == ''){
              $parentTag.addClass('error').append('&lt;span class=&quot;error&quot;&gt;Required field&lt;/span&gt;');
          }

      // Run the email validation using the regex for those input items also having class &quot;email&quot;
	  if($(this).hasClass('email') == true){
              if(!emailReg.test(inputVal)){
                  $parentTag.addClass('error').append('&lt;span class=&quot;error&quot;&gt;Enter a valid email address.&lt;/span&gt;');
              }
          }
    });

    // All validation complete - check whether any errors exist - if not submit form
       if ($('span.error').length == &quot;0&quot;) {
           $formId.append($loading.clone());
           $('fieldset',$formId).hide();
           $.post(formAction, $formId.serialize(),function(data){
               $('.loading').remove();
               $formId.append(data).fadeIn();
           });
      }
    // Use the following to prevent the form being submitted the standard way
      e.preventDefault();
});
</pre>
<p>When the submit button is clicked all input fields with the class &#8220;required&#8221; are checked to see if they have a value. If not an error class is applied to the parent list tag (allows us to highlight the complete form row) as well as an error text message inserted after the input field.</p>
<p>If the input field also has the class &#8220;email&#8221; a further validation check is made using a standard regular expression to see if the text pattern matches that of an email format.</p>
<p>If any of the validation checks return an error then the form submit is stopped. Here we use an extremely easy and effective way of seeing if there are any errors by using $(&#8216;span.error&#8217;).length. If length is not &#8220;0&#8243; then there are error messages still active.</p>
<p>If all validation is clear we then use the $.post function to submit the form data. At the same time we hide the form contents and replace with a loading gif.</p>
<p>When the data is returned from the AJAX post we then hide the loading gif and replace the form contents with the data itself &#8211; usually this would be something like a status message for the user.</p>
<h3><strong>The Complete jQuery Code</strong></h3>
<pre class="brush: jscript; title: ; notranslate">
jQuery(document).ready(function($) {
	var $loading = $('&lt;div class=&quot;loading&quot;&gt;&lt;img src=&quot;/media/images/loading.gif&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;');
	$(&quot;.default&quot;).each(function(){
		var defaultVal = $(this).attr('title');
		$(this).focus(function(){
			if ($(this).val() == defaultVal){
				$(this).removeClass('active').val('');
			}
		});
		$(this).blur(function() {
			if ($(this).val() == ''){
				$(this).addClass('active').val(defaultVal);
			}
		})
		.blur().addClass('active');
	});
	$('.btn-submit').click(function(e){
		var $formId = $(this).parents('form');
		var formAction = $formId.attr('action');
		defaulttextRemove();
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		$('li',$formId).removeClass('error');
		$('span.error').remove();
		$('.required',$formId).each(function(){
			var inputVal = $(this).val();
			var $parentTag = $(this).parent();
			if(inputVal == ''){
				$parentTag.addClass('error').append('&lt;span class=&quot;error&quot;&gt;Required field&lt;/span&gt;');
			}
			if($(this).hasClass('email') == true){
				if(!emailReg.test(inputVal)){
					$parentTag.addClass('error').append('&lt;span class=&quot;error&quot;&gt;Enter a valid email address.&lt;/span&gt;');
				}
			}
		});
		if ($('span.error').length == &quot;0&quot;) {
			$formId.append($loading.clone());
			$('fieldset',$formId).hide();
			$.post(formAction, $formId.serialize(),function(data){
				$('.loading').remove();
				$formId.append(data).fadeIn();
			});
		}
		e.preventDefault();
	});
});
function defaulttextRemove(){
	$('.default').each(function(){
		var defaultVal = $(this).attr('title');
		if ($(this).val() == defaultVal){
			$(this).val('');
		}
	});
}
</pre>
<p><a href="/lab/jquery/demo/jquery_ajax_form_submit_with_validation.htm" class="demo">View Demo</a></p>
<p><a href="http://www.designchemical.com/lab/jquery/demo/download/jquery_ajax_form_submit_with_validation.zip" class="demo">Download Source Files</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.designchemical.com/blog/index.php/jquery/create-your-own-jquery-ajax-form-submit-with-validation/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Check Passwords Using jQuery</title>
		<link>http://www.designchemical.com/blog/index.php/jquery/check-passwords-using-jquery/</link>
		<comments>http://www.designchemical.com/blog/index.php/jquery/check-passwords-using-jquery/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 18:00:29 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[Web Design Tips]]></category>
		<category><![CDATA[Website Coding]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.designchemical.com/blog/?p=387</guid>
		<description><![CDATA[<a href="http://www.designchemical.com/blog/index.php/jquery/check-passwords-using-jquery/"><img align="left" hspace="5" width="120" height="120" src="http://www.designchemical.com/blog/wp-content/uploads/2010/05/thumb_jquery_code.png" class="alignleft tfe wp-post-image" alt="thumb_jquery_code" title="thumb_jquery_code" /></a><p>We have had several requests on how to use jQuery to check that passwords match in your website registration form when requesting visitors to re-enter their passwords.</p>
<p>This can easily be achieved by using similar code shown in our previous posts on Using jQuery to Validate Search Box Input and Email Validation Using jQuery.</p>
1. The Password Input Fields
2.<p> &#8230; <a href="http://www.designchemical.com/blog/index.php/jquery/check-passwords-using-jquery/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<p>We have had several requests on how to use jQuery to check that passwords match in your website registration form when requesting visitors to re-enter their passwords.</p>
<p>This can easily be achieved by using similar code shown in our previous posts on <a href="/blog/index.php/jquery/using-jquery-to-validate-search-box-input/">Using jQuery to Validate Search Box Input</a> and <a href="/blog/index.php/jquery/email-validation-using-jquery/">Email Validation Using jQuery</a>.</p>
<h3>1. The Password Input Fields</h3>
<pre class="brush: plain; title: ; notranslate">
&lt;form method=&quot;post&quot; name=&quot;form1&quot; id=&quot;form-password&quot; action=&quot;&quot;&gt;
  &lt;fieldset&gt;
    &lt;label&gt;Password:&lt;/label&gt;
    &lt;input type=&quot;password&quot; name=&quot;password&quot; id=&quot;password&quot; value=&quot;&quot; size=&quot;32&quot; /&gt;
    &lt;label&gt;Re-Enter Password:&lt;/label&gt;
    &lt;input type=&quot;password&quot; name=&quot;password-check&quot; id=&quot;password-check&quot; value=&quot;&quot; size=&quot;32&quot; /&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; id=&quot;submit&quot;&gt;
  &lt;/fieldset&gt;
&lt;/form&gt;
</pre>
<h3>2. The jQuery Validation Code &#8211; Check That Both Passwords Match</h3>
<pre class="brush: jscript; title: ; notranslate">
jQuery(function(){
        $(&quot;#submit&quot;).click(function(){
        $(&quot;.error&quot;).hide();
        var hasError = false;
        var passwordVal = $(&quot;#password&quot;).val();
        var checkVal = $(&quot;#password-check&quot;).val();
        if (passwordVal == '') {
            $(&quot;#password&quot;).after('&lt;span class=&quot;error&quot;&gt;Please enter a password.&lt;/span&gt;');
            hasError = true;
        } else if (checkVal == '') {
            $(&quot;#password-check&quot;).after('&lt;span class=&quot;error&quot;&gt;Please re-enter your password.&lt;/span&gt;');
            hasError = true;
        } else if (passwordVal != checkVal ) {
            $(&quot;#password-check&quot;).after('&lt;span class=&quot;error&quot;&gt;Passwords do not match.&lt;/span&gt;');
            hasError = true;
        }
        if(hasError == true) {return false;}
    });
});
</pre>
<p>The above code will perform the following checks upon clicking the submit button:</p>
<ul>
<li>Check that a password has been entered and return an error message if the input field is empty.</li>
<li>Check that the &#8220;re-enter password&#8221; field contains text and return an error message if the input field is empty.</li>
<li>Finally &#8211; compare both password texts to check if they match</li>
</ul>
<p>If any of the validation checks return an error then the form submission on your website is stopped with the &#8220;return false&#8221; command.</p>
<p><a href="/lab/jquery/demo/check_passwords_using_jquery.htm" class="demo">Check out the demo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designchemical.com/blog/index.php/jquery/check-passwords-using-jquery/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Using jQuery to Validate Search Box Input</title>
		<link>http://www.designchemical.com/blog/index.php/jquery/using-jquery-to-validate-search-box-input/</link>
		<comments>http://www.designchemical.com/blog/index.php/jquery/using-jquery-to-validate-search-box-input/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 20:18:08 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[Website Coding]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.designchemical.com/blog/?p=383</guid>
		<description><![CDATA[<a href="http://www.designchemical.com/blog/index.php/jquery/using-jquery-to-validate-search-box-input/"><img align="left" hspace="5" width="120" height="120" src="http://www.designchemical.com/blog/wp-content/uploads/2010/05/thumb_jquery_code.png" class="alignleft tfe wp-post-image" alt="thumb_jquery_code" title="thumb_jquery_code" /></a><p>Today&#8217;s tip is just another simple example of how you can use the versatile jQuery library to validate form input text in your website.</p>
<p>In a recent project a client required validation on a search form input that would only allow numbers, letters and a &#8220;-&#8221;.</p>
The Search Form
The jQuery Validation Code
<p>The above code will first check that &#8230; <a href="http://www.designchemical.com/blog/index.php/jquery/using-jquery-to-validate-search-box-input/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<p>Today&#8217;s tip is just another simple example of how you can use the versatile jQuery library to validate form input text in your website.</p>
<p>In a recent project a client required validation on a search form input that would only allow numbers, letters and a &#8220;-&#8221;.</p>
<h3>The Search Form</h3>
<pre class="brush: plain; title: ; notranslate">
&lt;form method=&quot;post&quot; name=&quot;form1&quot; action=&quot;&quot;&gt;
     &lt;fieldset&gt;
          &lt;div&gt;&lt;label&gt;Search:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;search-text&quot; id=&quot;search-text&quot; value=&quot;&quot; size=&quot;32&quot; /&gt;&lt;/div&gt;
          &lt;div&gt;&lt;label&gt;&lt;/label&gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot; id=&quot;btn-search&quot; /&gt;&lt;/div&gt;
     &lt;/fieldset&gt;
&lt;/form&gt;
</pre>
<h3>The jQuery Validation Code</h3>
<pre class="brush: jscript; title: ; notranslate">
jQuery(function(){
        $(&quot;#btn-search&quot;).click(function(){
        $(&quot;.error&quot;).hide();
        var hasError = false;
        var searchReg = /^[a-zA-Z0-9-]+$/;
        var searchVal = $(&quot;#search-text&quot;).val();
        if(searchVal == '') {
            $(&quot;#search-text&quot;).after('&lt;span class=&quot;error&quot;&gt;Please enter a search term.&lt;/span&gt;');
            hasError = true;
        } else if(!searchReg.test(searchVal)) {
            $(&quot;#search-text&quot;).after('&lt;span class=&quot;error&quot;&gt;Enter valid text.&lt;/span&gt;');
            hasError = true;
        }
        if(hasError == true) {return false;}
    });
});
</pre>
<p>The above code will first check that text has been entered and return an error message if the search box is empty. Then check, using a simple regular expression, that the entered text only contains alphanumeric characters or a &#8220;-&#8221;. If either of these validation checks return and error message then the form submission is stopped with the &#8220;return false&#8221; command.</p>
<p><a href="/lab/jquery/demo/jquery_search_validation.htm" class="demo">Check out the demo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designchemical.com/blog/index.php/jquery/using-jquery-to-validate-search-box-input/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Email Validation Using jQuery</title>
		<link>http://www.designchemical.com/blog/index.php/jquery/email-validation-using-jquery/</link>
		<comments>http://www.designchemical.com/blog/index.php/jquery/email-validation-using-jquery/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 14:10:49 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[Website Coding]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[regex]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.designchemical.com/blog/?p=326</guid>
		<description><![CDATA[<a href="http://www.designchemical.com/blog/index.php/jquery/email-validation-using-jquery/"><img align="left" hspace="5" width="120" height="120" src="http://www.designchemical.com/blog/wp-content/uploads/2010/05/thumb_jquery_code.png" class="alignleft tfe wp-post-image" alt="thumb_jquery_code" title="thumb_jquery_code" /></a><p>A client recently wanted to include validation on their website registration form that would not only check that the input text was a genuine email address but also to disallow registration of any &#8220;free&#8221; email addresses.</p>
<p>Using jQuery we can easily add this to the website by creating a regular expression, which checks for email addresses such as yahoo, gmail, &#8230; <a href="http://www.designchemical.com/blog/index.php/jquery/email-validation-using-jquery/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<p>A client recently wanted to include validation on their website registration form that would not only check that the input text was a genuine email address but also to disallow registration of any &#8220;free&#8221; email addresses.</p>
<p>Using jQuery we can easily add this to the website by creating a regular expression, which checks for email addresses such as yahoo, gmail, hotmail, etc prior to submitting the form to the server. The regular expression can be adapted to reject any domain specified.</p>
<p>First add the form to your website using standard HTML code:</p>
<pre class="brush: plain; title: ; notranslate">
&lt;form method=&quot;post&quot; name=&quot;form1&quot; action=&quot;&quot;&gt;
  &lt;fieldset&gt;
   &lt;label&gt;Email Address:&lt;/labe&gt;
   &lt;input type=&quot;text&quot; name=&quot;UserEmail&quot; id=&quot;UserEmail&quot; value=&quot;&quot; size=&quot;32&quot; /&gt;
   &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; id=&quot;btn-submit&quot; /&gt;
  &lt;/fieldset&gt;
&lt;/form&gt;
</pre>
<h3>Step 1 &#8211; Initiate the jQuery Validation</h3>
<p><a href="http://www.designchemical.com/blog/index.php/jquery/adding-jquery-to-your-website/">After adding jQuery to your website</a> we set up the code that will perform the validation when the submit button is clicked &#8211; first clearing any existing error messages from previous attempts:</p>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(function() { 

    $('#btn-submit').click(function() {  

        $(&quot;.error&quot;).hide();
        var hasError = false;

    });
});
</pre>
<h3>Step 2 &#8211; Check that the Input Field is Not Empty</h3>
<p>For the first step of the validation we want to add the jQuery code that will check if there is any text in the input field and if not return the appropriate error message and stop the form being submitted:</p>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(function() { 

    $('#btn-submit').click(function() {  

        $(&quot;.error&quot;).hide();
        var hasError = false;

        var emailaddressVal = $(&quot;#UserEmail&quot;).val();
        if(emailaddressVal == '') {
            $(&quot;#UserEmail&quot;).after('&lt;span class=&quot;error&quot;&gt;Please enter your email address.&lt;/span&gt;');
            hasError = true;
        }

        if(hasError == true) { return false; }

    });
});
</pre>
<div class="banner-horizontal">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-7740611438413740";
/* Banner Mid Post */
google_ad_slot = "8625321051";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
<h3>Step 3 &#8211; Check that the Entered Text is a Valid Email Address</h3>
<p>Our first regular expression will check to make sure that the entered text is a valid email address &#8211; i.e. contains at least one &#8220;.&#8221;, an &#8220;@&#8221; symbol and an appropriate top-level domain suffix (e.g. .com, .net, etc):</p>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(function() { 

    $('#btn-submit').click(function() {  

        $(&quot;.error&quot;).hide();
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailaddressVal = $(&quot;#UserEmail&quot;).val();
        if(emailaddressVal == '') {
            $(&quot;#UserEmail&quot;).after('&lt;span class=&quot;error&quot;&gt;Please enter your email address.&lt;/span&gt;');
            hasError = true;
        }

        else if(!emailReg.test(emailaddressVal)) {
            $(&quot;#UserEmail&quot;).after('&lt;span class=&quot;error&quot;&gt;Enter a valid email address.&lt;/span&gt;');
            hasError = true;
        }

        if(hasError == true) { return false; }

    });
});
</pre>
<h3>Step 4 &#8211; Block or Prevent Free Email Addresses &#8211; e.g. yahoo.com, gmail.com and hotmail.com</h3>
<p>The 2nd regular expression will now check the entered text against our pre-defined domain names &#8211; in this case yahoo.com, gmail.com and hotmail.com. Once again an error message is displayed and the form submission stopped if the email address matches one of the domains:</p>
<pre class="brush: jscript; title: ; notranslate">
$(document).ready(function() { 

  $('#btn-submit').click(function() {  

    $(&quot;.error&quot;).hide();
    var hasError = false;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    var emailblockReg =
     /^([\w-\.]+@(?!gmail.com)(?!yahoo.com)(?!hotmail.com)([\w-]+\.)+[\w-]{2,4})?$/;

    var emailaddressVal = $(&quot;#UserEmail&quot;).val();
    if(emailaddressVal == '') {
      $(&quot;#UserEmail&quot;).after('&lt;span class=&quot;error&quot;&gt;Please enter your email address.&lt;/span&gt;');
      hasError = true;
    }

    else if(!emailReg.test(emailaddressVal)) {
      $(&quot;#UserEmail&quot;).after('&lt;span class=&quot;error&quot;&gt;Enter a valid email address.&lt;/span&gt;');
      hasError = true;
    }

    else if(!emailblockReg.test(emailaddressVal)) {
      $(&quot;#UserEmail&quot;).after('&lt;span class=&quot;error&quot;&gt;No yahoo, gmail or hotmail emails.&lt;/span&gt;');
      hasError = true
    } 

    if(hasError == true) { return false; }

    });
});
</pre>
<p>The above example can easily be configured to reject (or even accept) any specified email address. To see it in action:</p>
<p class="clear-float"><a href="/lab/jquery/demo/jquery_email_validation.htm" title="jQuery Demo - Email Validation Using jQuery" class="demo">Check out our online demo of Email Validation Using jQuery</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designchemical.com/blog/index.php/jquery/email-validation-using-jquery/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Benefits of Website HTML &amp; CSS Validation</title>
		<link>http://www.designchemical.com/blog/index.php/website-coding/benefits-of-website-html-css-validation/</link>
		<comments>http://www.designchemical.com/blog/index.php/website-coding/benefits-of-website-html-css-validation/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 16:31:54 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[Website Coding]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://www.designchemical.com/blog/?p=56</guid>
		<description><![CDATA[<a href="http://www.designchemical.com/blog/index.php/website-coding/benefits-of-website-html-css-validation/"><img align="left" hspace="5" width="150" src="http://www.designchemical.com/blog/wp-content/plugins/thumbnail-for-excerpts/jquery_code.png" class="alignleft wp-post-image tfe" alt="" title="" /></a><p>Many people ask the question &#8230; why validate?</p>
<p>Although most modern browsers will still display code with errors, validation ensures that your web pages meet current HTML and CSS standards. From a web designers point of view validation is a quick and easy way to check for errors that may be causing issues with the way your website renders on &#8230; <a href="http://www.designchemical.com/blog/index.php/website-coding/benefits-of-website-html-css-validation/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<p>Many people ask the question &#8230; why validate?</p>
<p>Although most modern browsers will still display code with errors, validation ensures that your web pages meet current HTML and CSS standards. From a web designers point of view validation is a quick and easy way to check for errors that may be causing issues with the way your website renders on the screen.</p>
<p>There are however several areas where website validation is important for your site:</p>
<h3><strong>Website accessibility</strong></h3>
<p>An increasingly important aspect of web design is to develop and create websites where all of the information can be accessed by all visitors &#8230; afterall why build a website for your business if a significant portion of internet users cannot access the content?</p>
<p>The validation process helps identify potential areas where issues with accessibility may occur and removes possible &#8220;barriers&#8221; to visitors who may be vision impaired, motor impaired or cognitive impaired.</p>
<p>For many countries this is now becoming a legal requirement and it is important for both clients and web designers to ensure that your websites do not discriminate.</p>
<h3><strong>Search Engine Optimization</strong></h3>
<p>Search engine spiders use the structure of your web page to identify different components of your content to help with indexing. Whilst errors in code may not stop spiders in their tracks, code that is not correctly formed, e.g. <h> tags that are not closed or even tags that do not technically exist, may result in the search engines missing out on important keywords or phrases that identify your content.</p>
<p>At the end of the day your goal is to drive traffic to your website through organic search results. It would be unfortunate if all of your hard work developing valuable content is negated by simple errors in the HTML code.</p>
<p>Check out our <a href="http://www.designchemical.com/blog/index.php/search-engine-optimization/seo-alchemy-the-importance-of-good-code/">Search engine optimization and the importance of good code</a> blog post for more information.</p>
<h3><strong>Faster Web Page Loading Times</strong></h3>
<p>Validated code will help ensure that your browser renders the page as quickly as possible on the screen. In a world where the patience of internet users is constantly decreasing every second helps in guaranteeing that your visitors stay on your website.</p>
<h3><strong>Browser Compatibility</strong></h3>
<p>Website coding standards take into account future expected developments so although most modern browsers can handle errors in code, validation will help guarantee that as browsers develop your website will be &#8220;forwards compatible&#8221; and continue to function and render on the screen as originally intended.</p>
<p>Website validation helps reduce the possibility of having to hire a web designer for potentially costly rewrites of your web page in the not too distant future.</p>
<h3><strong>Updating Website Code</strong></h3>
<p>For web designers, updating other peoples code can be a frustrating a time consuming experience, spending several hours just to correct unecessary errors that should have not occurred in the first place. Creating semantically meaningful, valid HTML and CSS from day one will help your clients update their website in the future and ultimately minimize costs.</p>
<h3><strong>Be Professional With Your Web Design</strong></h3>
<p>From a web designers point of view, lets not forget one important aspect &#8230; creating valid, standards compliant XHTML and CSS is a sign of being professional. Whilst a website may look shiny on the surface, what&#8217;s below is equally important.</p>
<p>It is true however that validation does not guarantee that your pages will work in all browsers, neither does it mean that the XHTML and CSS used in the web page layout is appropriate but it is certainly a start to ensuring that your code is optimized and standards compliant. As a client you should expect your code to always validate.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designchemical.com/blog/index.php/website-coding/benefits-of-website-html-css-validation/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Website HTML and CSS Validation</title>
		<link>http://www.designchemical.com/blog/index.php/validation/website-html-and-css-validation/</link>
		<comments>http://www.designchemical.com/blog/index.php/validation/website-html-and-css-validation/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 16:37:51 +0000</pubDate>
		<dc:creator>Lee</dc:creator>
				<category><![CDATA[Validation]]></category>
		<category><![CDATA[Website Coding]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://www.designchemical.com/blog/?p=32</guid>
		<description><![CDATA[<a href="http://www.designchemical.com/blog/index.php/validation/website-html-and-css-validation/"><img align="left" hspace="5" width="150" src="http://www.designchemical.com/blog/wp-content/plugins/thumbnail-for-excerpts/jquery_code.png" class="alignleft wp-post-image tfe" alt="" title="" /></a><p>I was amazed to recently read on one local forum someone advising that a web design company with a website that has less than 100 errors could be considered as &#8220;decent&#8221; &#8230; crazy &#8230; there is absolutely no reason at all for any errors or warnings in website code.</p>
<p>I am always shocked by how few locally designed websites in &#8230; <a href="http://www.designchemical.com/blog/index.php/validation/website-html-and-css-validation/" class="read_more">more</a></p>]]></description>
			<content:encoded><![CDATA[<p>I was amazed to recently read on one local forum someone advising that a web design company with a website that has less than 100 errors could be considered as &#8220;decent&#8221; &#8230; crazy &#8230; there is absolutely no reason at all for any errors or warnings in website code.</p>
<p>I am always shocked by how few locally designed websites in Bangkok validate. Validation is a relatively simple process which allows you to easily check the code of your website to make sure that it meets the latest standards.</p>
<p>I have found that the best service to use for validation is <a href="http://validator.w3.org">http://validator.w3.org</a>.</p>
<p>It is an excellent way for clients to check potential web design companies to see whether their knowledge of websites and coding goes further than just the surface &#8211; simply check the web design company&#8217;s own website!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designchemical.com/blog/index.php/validation/website-html-and-css-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

