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.
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.
If you want to skip ahead, Check out the demo and download the plugin from GitHub.
Start with the Markup
To get started, I would always start with a simple form marked up in an ordered list:
<form action="thanks.htm" method="post">
<ol class="forms">
<li>
<label for="name"><em class="required">*</em> Name</label>
<input type="text" name="name" id="name" />
</li>
<li>
<label for="email"><em class="required">*</em> Email</label>
<input type="text" name="email" id="email" />
</li>
<li>
<label for="website">Website</label>
<input type="text" name="website" id="website" />
</li>
<li>
<label for="comment"><em class="required">*</em> Comment</label>
<textarea name="comment" id="comment"></textarea>
</li>
<li class="buttons"><button type="submit">Submit</button></li>
</ol>
</form>
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 required to each field that needs to be validated.
<form action="thanks.htm" method="post">
<ol class="forms">
<li>
<label for="name"><em class="required">*</em> Name</label>
<input type="text" name="name" id="name" class="required" />
</li>
<li>
<label for="email"><em class="required">*</em> Email</label>
<input type="text" name="email" id="email" class="required" />
</li>
<li>
<label for="website">Website</label>
<input type="text" name="website" id="website" />
</li>
<li>
<label for="comment"><em class="required">*</em> Comment</label>
<textarea name="comment" id="comment" class="required"></textarea>
</li>
<li class="buttons"><button type="submit">Submit</button></li>
</ol>
</form>
Since I have an email field, I also need to add the class of email to the email field:
<form action="thanks.htm" method="post">
<ol class="forms">
<li>
<label for="name"><em class="required">*</em> Name</label>
<input type="text" name="name" id="name" class="required" />
</li>
<li>
<label for="email"><em class="required">*</em> Email</label>
<input type="text" name="email" id="email" class="required email" />
</li>
<li>
<label for="website">Website</label>
<input type="text" name="website" id="website" />
</li>
<li>
<label for="comment"><em class="required">*</em> Comment</label>
<textarea name="comment" id="comment" class="required"></textarea>
</li>
<li class="buttons"><button type="submit">Submit</button></li>
</ol>
</form>
I also want to tell my plugin which forms to validate, so I’ll also add a class to the form itself:
<form action="thanks.htm" method="post" class="required-form">
<ol class="forms">
<li>
<label for="name"><em class="required">*</em> Name</label>
<input type="text" name="name" id="name" class="required" />
</li>
<li>
<label for="email"><em class="required">*</em> Email</label>
<input type="text" name="email" id="email" class="required email" />
</li>
<li>
<label for="website">Website</label>
<input type="text" name="website" id="website" />
</li>
<li>
<label for="comment"><em class="required">*</em> Comment</label>
<textarea name="comment" id="comment" class="required"></textarea>
</li>
<li class="buttons"><button type="submit">Submit</button></li>
</ol>
</form>
It’s JavaScript Time
Get started by including jQuery and my Simple Validate plugin. Then, include them on the page:
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.simpleValidate.min.js"></script>
Now, I just need to call my plugin on any form that has required fields:
$(document).ready(function() {$('form.required-form').simpleValidate();});
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 <em> tag instead of a <strong> tag:
$(document).ready(function() {
$('form.required-form').simpleValidate({
errorElement: 'em'
});
});
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:
$(document).ready(function() {
$('form.required-form').simpleValidate({
errorElement: 'em',
ajaxRequest: true,
completeCallback: function($el) {
var formData = $el.serialize();
//Do AJAX request with formData variable
}
});
});
Options
Here are all of the available options for the plugin:
errorClass: 'error'
Class for the error message texterrorText: '{label} is a required field.'
Structure for the error message text, {label} will be replaced with the associated label textemailErrorText: 'Please enter a valid {label}'
Structure for the email error message text, {label} will be replaced with the associated label texterrorElement: 'strong'
Element to use for the error message textremoveLabelChar: '*'
If there is an extra character in the label to denote a required field, strip it outinputErrorClass: ''
Class to add to an input when it is marked as having an errorcompleteCallback: ''
Function to call once the form is error-freeajaxRequest: false
If you don't want to use the default form action and want to submit it via AJAX
Check out the demo and download the plugin from GitHub. Let me know if you run into any bugs or have any feature requests.
Categories
Recent Articles
May 2012
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 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 |
19 Comments
ceasar
07.21.2010real nice and small
One question. I would like to add a number validation. How can that be done ?
This is what I have but doesn’t work
else if ($input.hasClass(‘number’)) {
if(!(/^\d+$/.test(fieldValue))) {
errorMsg = (errorMsgType > -1 ) ? errorMsg = o.numberErrorText.replace(’{label}’,labelText) : errorMsg = o.numberErrorText;
hasError = true;
}
ceasar
07.21.2010I think I found the problem.
The minified version is different from the original version
Trevor
07.21.2010@Ceasar-
What do you mean the minified version is different from the original?
ceasar
07.21.2010If you unscramble the minified js you will see a difference with the not original version
Trevor
07.21.2010@Ceasar-
Is there some functionality that is different between the two?
Sam Dean
11.29.2010I think the minified version is different to the non-minified version because if i use the minified version then the script didn’t work. All i did was change it to the non-minified version and it works
Trevor
11.29.2010@Sam Dean-
What error are you getting? What version of jQuery are you using? I am using the minified version just fine in the demo.
Richy
01.08.2011Hi Trevor. I have been playing around with your form, and it looks great. I am very new to this and I am a bit confused. Where do I put my email address so I can receive the emails from the form. A stupid question I know, but as I said, I am very new to this. Thanks in advance.
Trevor
01.09.2011@Richy-
You will need to have some server side code (PHP, Rails, etc) to process the form. This plugin just does client side validation.
nady
01.22.2011how validation
optionorselectnot justinputTrevor
01.22.2011@Nady-
Selects have a value just like any input would.
Steve
01.23.2011Nice plugin Trevor. 2 questions.
Can I set for the name input to require a minimum of 2 characters to be accepted as valid?
And can I add icons to the errors em elements as background images with css?
Thanks.
Trevor
01.23.2011@Steve-
Sure, you would just need to modify the script to require a minimum of 2 characters to be valid.
You can certainly add background images with CSS to the error messages.
Gerry Danen
02.10.2011Sorry, but I don’t see where $el comes from in
completeCallback: function($el)
I am getting undefined variables.
Trevor
02.10.2011@Gerry Danen-
$el is the form being passed back to the callback function. You can name it whatever you want.
Gerry Danen
02.10.2011Thanks for the response, Trevor.
I thought it was the info coming back, yet I get the “undefined variable” error.
Could you have a look at the site?
Trevor
02.10.2011@Gerry Danen-
Those are PHP errors you are getting. This plugin is JavaScript only, so I have no clue what you’ve got going on in your PHP.
Rey
04.23.2011Hi Trevor,
Thanks for the awesome script! A question, how do I check if the message/name has enough words or characters in it?
Trevor
04.24.2011@Rey-
That’s not something you can do by default with the plugin. But feel free to fork it and add whatever functionality you need.
Too late, comments are closed!
Don’t worry, you can email me or contact me on Twitter.