<p>I find that I end up consistently adding form validation to sites, and for some reason, I never made a plugin out of it. I feel like every jQuery validation plugin that I look at is way too bloated for what I really need, so I finally whipped up a simple validation plugin that comes in at about 1.5kb.</p>
<p>I tried to keep it really simple with this plugin: just check to see if a field that has been marked as required is not empty and check to see if an email address is valid.</p>
<p>If you want to skip ahead, <a href="//assets.trevor-davis.com/play/jQuery-Simple-Validate/">Check out the demo</a> and <a href="http://github.com/davist11/jQuery-Simple-Validate">download the plugin from GitHub</a>.</p>

<h2>Start with the Markup</h2>
<p>To get started, I would always start with a simple form marked up in an ordered list:</p>

<pre><code>&lt;form action="thanks.htm" method="post"&gt;
	&lt;ol class="forms"&gt;
		&lt;li&gt;
			&lt;label for="name"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Name&lt;/label&gt;
			&lt;input type="text" name="name" id="name" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="email"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Email&lt;/label&gt;
			&lt;input type="text" name="email" id="email" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="website"&gt;Website&lt;/label&gt;
			&lt;input type="text" name="website" id="website" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="comment"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Comment&lt;/label&gt;
			&lt;textarea name="comment" id="comment"&gt;&lt;/textarea&gt;
		&lt;/li&gt;
		&lt;li class="buttons"&gt;&lt;button type="submit"&gt;Submit&lt;/button&gt;&lt;/li&gt;
	&lt;/ol&gt;
&lt;/form&gt;</code></pre>

<p>Then, I need some way to denote which fields should be validated. The HTML5 required attribute, just isn’t supported well enough to use, so all that is needed is to add the class of <strong>required</strong> to each field that needs to be validated.</p>

<pre><code>&lt;form action="thanks.htm" method="post"&gt;
	&lt;ol class="forms"&gt;
		&lt;li&gt;
			&lt;label for="name"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Name&lt;/label&gt;
			&lt;input type="text" name="name" id="name" <strong>class="required"</strong> /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="email"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Email&lt;/label&gt;
			&lt;input type="text" name="email" id="email" <strong>class="required"</strong> /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="website"&gt;Website&lt;/label&gt;
			&lt;input type="text" name="website" id="website" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="comment"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Comment&lt;/label&gt;
			&lt;textarea name="comment" id="comment" <strong>class="required"</strong>&gt;&lt;/textarea&gt;
		&lt;/li&gt;
		&lt;li class="buttons"&gt;&lt;button type="submit"&gt;Submit&lt;/button&gt;&lt;/li&gt;
	&lt;/ol&gt;
&lt;/form&gt;</code></pre>

<p>Since I have an email field, I also need to add the class of <strong>email</strong> to the email field:</p>

<pre><code>&lt;form action="thanks.htm" method="post"&gt;
	&lt;ol class="forms"&gt;
		&lt;li&gt;
			&lt;label for="name"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Name&lt;/label&gt;
			&lt;input type="text" name="name" id="name" class="required" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="email"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Email&lt;/label&gt;
			&lt;input type="text" name="email" id="email" class="required <strong>email</strong>" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="website"&gt;Website&lt;/label&gt;
			&lt;input type="text" name="website" id="website" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="comment"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Comment&lt;/label&gt;
			&lt;textarea name="comment" id="comment" class="required"&gt;&lt;/textarea&gt;
		&lt;/li&gt;
		&lt;li class="buttons"&gt;&lt;button type="submit"&gt;Submit&lt;/button&gt;&lt;/li&gt;
	&lt;/ol&gt;
&lt;/form&gt;</code></pre>

<p>I also want to tell my plugin which forms to validate, so I’ll also add a class to the form itself:</p>

<pre><code>&lt;form action="thanks.htm" method="post" <strong>class="required-form"</strong>&gt;
	&lt;ol class="forms"&gt;
		&lt;li&gt;
			&lt;label for="name"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Name&lt;/label&gt;
			&lt;input type="text" name="name" id="name" class="required" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="email"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Email&lt;/label&gt;
			&lt;input type="text" name="email" id="email" class="required email" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="website"&gt;Website&lt;/label&gt;
			&lt;input type="text" name="website" id="website" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="comment"&gt;&lt;em class="required"&gt;*&lt;/em&gt; Comment&lt;/label&gt;
			&lt;textarea name="comment" id="comment" class="required"&gt;&lt;/textarea&gt;
		&lt;/li&gt;
		&lt;li class="buttons"&gt;&lt;button type="submit"&gt;Submit&lt;/button&gt;&lt;/li&gt;
	&lt;/ol&gt;
&lt;/form&gt;</code></pre>

<h2>It’s JavaScript Time</h2>
<p>Get started by including <a href="http://jquery.com/">jQuery</a> and <a href="http://github.com/davist11/jQuery-Simple-Validate">my Simple Validate plugin</a>. Then, include them on the page:</p>

<pre><code>&lt;script type="text/javascript" src="scripts/jquery.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="scripts/jquery.simpleValidate.min.js"&gt;&lt;/script&gt;</code></pre>

<p>Now, I just need to call my plugin on any form that has required fields:</p>
<pre><code>$(document).ready(function() {
	$('form.required-form').simpleValidate();
});</code></pre>

<p>Just like that, the form validation will work, but I want to customize a few of the options. First, I want to change the error element to use an &lt;em&gt; tag instead of a &lt;strong&gt; tag:</p>

<pre><code>$(document).ready(function() {
	$('form.required-form').simpleValidate(<strong>{
		errorElement: 'em'
	}</strong>);
});</code></pre>

<p>Then, maybe I decide that I want to submit the form via AJAX. There is no need to add that kind of functionality into the plugin, AJAX is so easy with jQuery. So I just need to tell the plugin that I’m going to submit the form with AJAX, and then pass in my callback function which will fire once the form is submitted with no errors:</p>

<pre><code>$(document).ready(function() {
	$('form.required-form').simpleValidate({
		errorElement: 'em',
		<strong>ajaxRequest: true,
		completeCallback: function($el) {
			var formData = $el.serialize();
			//Do AJAX request with formData variable
		}</strong>
	});
});</code></pre>

<h3>Options</h3>
<p>Here are all of the available options for the plugin:</p>
<ul><li><h4>errorClass: 'error'</h4> Class for the error message text</li>
	<li><h4>errorText: '{label} is a required field.'</h4> Structure for the error message text, {label} will be replaced with the associated label text</li>
	<li><h4>emailErrorText: 'Please enter a valid {label}'</h4> Structure for the email error message text, {label} will be replaced with the associated label text</li>
	<li><h4>errorElement: 'strong'</h4> Element to use for the error message text</li>
	<li><h4>removeLabelChar: '*'</h4> If there is an extra character in the label to denote a required field, strip it out</li>
	<li><h4>inputErrorClass: ''</h4> Class to add to an input when it is marked as having an error</li>
	<li><h4>completeCallback: ''</h4> Function to call once the form is error-free</li>
	<li><h4>ajaxRequest: false</h4> If you don't want to use the default form action and want to submit it via AJAX 
</li></ul><p><a href="//assets.trevor-davis.com/play/jQuery-Simple-Validate/">Check out the demo</a> and <a href="http://github.com/davist11/jQuery-Simple-Validate">download the plugin from GitHub</a>. Let me know if you run into any bugs or have any feature requests.</p>