Last week I was performing a security review on a charity's web site (because that's how I roll, I'm nice like that) when I noticed they were using a jQuery plugin for forms validation. This was something I had not yet looked at in the jQuery world so I decided I'd take a closer look this weekend. I have to admit that I'm in awe over what I've found. I've avoided client side form validation for a long time (and I'll talk more about why in a minute) but I see no reason to do so anymore. What follows is a brief introduction into what I've learned. I'll be blogging a bit more on this topic later in the week with some more advanced examples.
2
3 <head>
4 <script src="/jquery/jquery.js"></script>
5 <script src="/jquery/jquery.validate.js"></script>
6 <script>
7 $(document).ready(function(){
8 $("#commentForm").validate();
9 });
10 </script>
11 </head>
12
13 <body>
14
15 <form id="commentForm" method="get" action="">
16 <fieldset>
17 <legend>A simple comment form with submit validation and default messages</legend>
18 <p>
19 <label for="cname">Name</label>
20 <em>*</em><input id="cname" name="name" size="25" class="required" />
21 </p>
22 <p>
23 <label for="cemail">E-Mail</label>
24 <em>*</em><input id="cemail" name="email" size="25" class="required email" />
25 </p>
26 <p>
27 <label for="ccomment">Your comment</label>
28 <em>*</em><textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
29 </p>
30 <p>
31 <input class="submit" type="submit" value="Submit"/>
32 </p>
33 </fieldset>
34 </form>
35
36
37 </body>
38 </html>
2 <label for="cbd">Birthday</label>
3 <em>*</em><input id="cbd" name="cbd" size="25" class="required date" />
4 </p>
5 <p>
6 <label for="curl">URL</label>
7 <em> </em><input id="curl" name="url" size="25" class="url" value="" />
8 </p>
2 <label for="cname">Name</label>
3 <em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
4 </p>
5 <p>
6 <label for="cage">Age</label>
7 <em>*</em><input id="cage" name="cage" size="4" class="required number" min="1" max="100" />
8 </p>
2 <label for="cfu">Bio Upload</label>
3 <em>*</em><input type="file" id="cfu" name="cfu" size="25" class="required" value="" accept="pdf" />
4 </p>
2 label.error { float: none; color: red; padding-left: .5em; vertical-align: top; font-weight:bold}
3 </style>
Comment 1 written by Neil Bailey on 9 February 2009, at 3:57 PM
Comment 2 written by Rick Mason on 9 February 2009, at 5:04 PM
The date validation isn't bullet proof. Try entering 5/22//52 and it doesn't catch it. Believe me that's a very common error in a date field.
Comment 3 written by Raymond Camden on 9 February 2009, at 5:11 PM
Comment 4 written by Gabe on 9 February 2009, at 5:49 PM
Comment 5 written by Gabe on 9 February 2009, at 6:05 PM
setTimeout(function() {
doSomething();
}, 500);
using the setTimeOut option for each validation in the document.
Comment 6 written by todd sharp on 9 February 2009, at 6:06 PM
Comment 7 written by denny on 9 February 2009, at 6:09 PM
And Dojo is geared for internationalization, and accessibility.
jQuery is getting there, but I love how dojo made it a huge part of the whole deal.
Probably un-needed in the grand scheme of things, I mean, who really needs multiple languages or accessibility? -- But still, I really think it's swell that Dojo made it an important part of their overall approach.
I just saw a blog post on using jQuery to do server-side validation, which looked interesting. If I come across it again I'll send you the link.
Sorry to keep saying "check out dojo", but I keep seeing people sorta re-inventing the wheel-- while leaving some pretty cool spokes off, as it were.
Guess I'm a fanboy of sorts. Heh. I'm down with that.
:-)
Comment 8 written by shag on 9 February 2009, at 6:18 PM
Comment 9 written by Scott Stroz on 9 February 2009, at 6:20 PM
Comment 10 written by Brian Swartzfager on 9 February 2009, at 6:22 PM
I have to agree with Rick, though: the date validation code in this Validation plugin has room for improvement. It failed my favorite date validation test, which is to see if it tags a non-existent date like Feb. 31 as being wrong. That's something a date picker can prevent merely by not giving you the option of picking that date.
Comment 11 written by denny on 9 February 2009, at 6:29 PM
Check this out: http://dojotoolkit.org/forum/dojo-and-html-doctype...
Basically, you can use pure JS or CSS if you're concerned with html validation, whatever the JS library.
Depends on how you want things to degrade and whatnot, too.
Comment 12 written by Raymond Camden on 9 February 2009, at 6:34 PM
@ToddSharp: Did I really? Hmmm, isn't that interesting. I'll have to include a link to the wishlist in the next example. ;)
@Denny: DOJO - I can only learn one thing at a time! ;)
@BrianS: Yep, when I was playing with the UI stuff last week or so, I did see the date picker and thought it was rather nice!
Comment 13 written by Scott Stroz on 9 February 2009, at 6:35 PM
Comment 14 written by existdissolve on 9 February 2009, at 8:46 PM
I've used Spry's validation tools a little--I like how simple they are, but the amount of markup (and JS includes) required is a bit overwhelming.
I like that this spits out error messages for you--the less typing, the better :)
Hopefully, Spry will steal some of the nice things here and implement them into future releases.
Comment 15 written by Raymond Camden on 9 February 2009, at 8:48 PM
Comment 16 written by Jeff Self on 9 February 2009, at 9:24 PM
Any way to set custom patterns? For instance, I'm working on a single field form that accepts either a 13 digit number or a 8 digit number. Anything else should be invalid. I've just started playing with jquery in the last week and I find it pretty cool.
Comment 17 written by Paul Sturm on 9 February 2009, at 9:30 PM
I've slipped the class fields into my code generator that builds my forms for a given database table. Then all I've got to do is create my $(document).ready()
Very unobtrusive. I should have guessed this was built by a jQuery team member since it is so 'elegant'.
By the way, your post form won't allow for email addresses with a '+' in it - as if that was invalid. I'll have to check and see if THIS plugin will allow it...
Comment 18 written by Raymond Camden on 9 February 2009, at 9:48 PM
@Paul Sturm: Sorry about that. That is a bug fixed in BlogCFC, but my blog install here is a bit out of date.
Comment 19 written by denny on 9 February 2009, at 9:54 PM
Extending jQuery validation:
http://tinyurl.com/5onkka
You'll be wanting to look at the regular expression stuff.
Something like this:
$.validator.addMethod('eightOr13', function (value) {
return /^((\d{13})|(\d{8}))$/.test(value);
}, 'Please enter a valid 8 or 13 digit code.');
I still like dojo's approach better tho. :-)P
@Ray: time is an interesting concept. :-)
Comment 20 written by Brad Wood on 9 February 2009, at 10:12 PM
Luckily, it's all open source so it was an easy addition.
Comment 21 written by Raymond Camden on 9 February 2009, at 10:17 PM
Comment 22 written by Sebastiaan on 10 February 2009, at 3:19 AM
Come up with the preso/demo ;-)
Comment 23 written by Seb Duggan on 10 February 2009, at 4:45 AM
I prefer to use tha alternate method: if you also add the jQuery metadata plugin - http://plugins.jquery.com/project/metadata - you can do something like this:
<p>
<label for="cage">Age</label>
<em>*</em><input id="cage" name="cage" size="4" class="required number {min:1,max:100}" />
</p>
Comment 24 written by Jörn Zaefferer on 10 February 2009, at 5:25 AM
About Gabe's issue in IE6 and document-ready: Have you tried placing your doSomething()-call in a script block just before the closing body-tag?
About localization: The plugin has english as the default language, but is bundled with 17 localizations (mostly contributions from other users).
About accessibility: The plugin uses labels with the correct for-attribute for the error messages, so a screenreader should be able to link the error-label to the associated input.
@Brad Wood: Would you mind sharing your addition?
Comment 25 written by Jeff Horne on 10 February 2009, at 7:59 AM
Unfortunately, most of the examples I find are with php and asp so I have to integrate it into cfm myself. Your ideas are very, very helpful, Ray. My latest adventure is to place a file upload form into a jquery accordion panel that stays in the panel after the upload. It's slow going.
Comment 26 written by Jeff Horne on 10 February 2009, at 8:06 AM
Comment 27 written by Raymond Camden on 10 February 2009, at 8:10 AM
Comment 28 written by Tony Nelson on 10 February 2009, at 8:55 AM
The inline validation seems convenient, but maybe not the most unobtrusive approach. If you think of validation as just another aspect of your application, having the validation rules (class="required" minlength="2") applied directly to the form fields isn't the best separation of concerns.
Granted the pragmatic side of me says this lightweight approach probably fits the need for most developers out there and that a more AOP-style validation engine, either database or XML driven, might be a little overkill.
Comment 29 written by Raymond Camden on 10 February 2009, at 8:58 AM
Comment 30 written by Lola LB on 10 February 2009, at 10:22 AM
Comment 31 written by Raymond Camden on 10 February 2009, at 10:25 AM
Comment 32 written by Neil Bailey on 10 February 2009, at 10:33 AM
We currently use the EXTJS package, and while its not stoopid large, its not tiny.
Why can't there be a single package that does every freaking thing........... <piss and moan>
Comment 33 written by Raymond Camden on 10 February 2009, at 10:36 AM
Both PHP and CF support reading files, but they certainly do it different ways. Ditto for the JS frameworks out there.
It is important that you (and by you I mean either you as the sole developer or you and your organization) find the library that enables you to be the most productive. (Frankly I think client download isn't a huge concern. jQuery has compressed versions and I'm sure EXT does as well. Spry does for sure.)
Comment 34 written by denny on 10 February 2009, at 7:23 PM
@Everyone/anyone:
jQuery seems to be going for the bottom-up on localization and accessibility, vs. dojo's top-down. I can appreciate the logic, and I feel that the only way it will work, is if devs guide other devs in "the right way" to code this stuff.
Make a big deal about being multi-lingual, and accessible (in general)!
Or maybe don't. Evolution will take care of all of it, in the end, come to think of it. Eh. =]
@Ray & Niel: Dojo has had /everything/ and /anything/ that I've needed to do with JS. Just amazing stuff. The build system, various tools for working with other languages... just far-out, wonderful stuff. I like how things are organized (the sources)... the list goes on and on.
Course, javascript is a pretty freaky language in and of itself, and just about any "helper" lib can do nifty things for it, so... guess whatever floats your boat, as with anything.
Swell to hear that localization and accessibility have been thought of with this jQuery validation plugin. I don't know why I like that so much, but I do. =]
Comment 35 written by Michael Williams on 11 February 2009, at 12:07 AM
Comment 36 written by Jose Galdamez on 12 February 2009, at 1:38 PM
That means we can't rely on <cfform> for any of its JS or AJAX-related features. Bummer!
After shopping around, I found jQuery to be a nice alternative. These demos make it seem even more enticing. In a way, the examples here are better than what one would get from <cfform> because you don't get the annoying alert() window. The error messages are much more friendly.
Comment 37 written by denny on 12 February 2009, at 3:55 PM
You can use the "scriptsrc" cfform attribute to specify a different directory to pull the JS and stuff from, bypassing a call to your IT department.
HIH
Comment 38 written by Chris on 16 February 2009, at 5:39 AM
I couldn't figure out from the documentation for the validation plug in how to make a text input field required when they choose a specific drop down box.
Basically I have a "how did you hear from us" drop down. If they choose "OTHER" I need to make the "OTHER" input field required.
If this is in the documentation, I'm sure I missed it for sure. Maybe a link to the page?
Thanks!
Comment 39 written by Raymond Camden on 16 February 2009, at 6:15 AM
Comment 40 written by Raymond Camden on 16 February 2009, at 6:28 AM
Comment 41 written by Raymond Camden on 16 February 2009, at 8:43 AM
http://www.coldfusionjedi.com/index.cfm/2009/2/16/...
Comment 42 written by Prabhjot singh on 3 April 2009, at 1:19 AM
Iam new to ASP.NET and jquery, i implemented the above written code in my .aspx pages. I have added the .js files onto the page. Now, whenever i run my project i get an error "jquery is undefined". I really don't know how to move forward. any help!!!
Comment 43 written by Raymond Camden on 3 April 2009, at 6:32 AM
Comment 44 written by prabhjot singh on 3 April 2009, at 7:23 AM
Comment 45 written by Raymond Camden on 3 April 2009, at 8:28 AM
<script src="/js/jquery.js"></script>
Is that the right path to the jquery folder? Does Firebug confirm the script has loaded?
Comment 46 written by prabhjot singh on 4 April 2009, at 1:18 AM
$("#form1").validate(); is undefined.
The code which i have written is as follows:
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery.validate.pack.js" type="text/javascript"></script>
<script src="jquery.validate.js" type="text/javascript"></script>
<script src="jquery.form.js" type="text/javascript"></script>
<script src="jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#form1").validate();
});
</script>
and then the form validations in the body tag. What more i have to do??
Regards
Prabhjot Singh
Comment 47 written by Raymond Camden on 4 April 2009, at 9:00 AM
Comment 48 written by prabhjot singh on 6 April 2009, at 12:14 AM
key is undefined
[Break on this error] var parts = key.split(".");
This error is pointing to jquery.js file. When i open the description i get this
data()()jquery.js (line 1355)
data()(input#cname.required)jquery.v...lidate.js (line 795)
rules()(input#cname.required)jquery.v...lidate.js (line 771)
(?)()()jquery.v...lidate.js (line 609)
(?)()()jquery.js (line 353)
grep()(Object length=3 0=input#cname.required, function(), undefined)jquery.js (line 1129)
filter()(function())jquery.js (line 352)
refresh()()jquery.v...lidate.js (line 605)
validator()(undefined, form#form1)jquery.v...lidate.js (line 397)
validate()(undefined)jquery.v...lidate.js (line 245)
(?)()()index.aspx (line 16)
(?)()()jquery.js (line 2905)
each()([function(), function()], function(), undefined)jquery.js (line 690)
ready()()jquery.js (line 2904)
(?)()()jquery.js (line 2929)
[Break on this error] var parts = key.split(".");
I have written down the same code as u. What next should i do??
Regards
prabhjot singh
Comment 49 written by prabhjot singh on 6 April 2009, at 4:21 AM
Everything is working just fine, Thanks for your help. Iam so happy
Comment 50 written by prabhjot singh on 6 April 2009, at 6:11 AM
i have to validate my first name and the last name field in such a way that no alpha-numeric character is entered. The "required" is working fine, but what about the other thing like i have mentioned. I tried by putting in a new function in jquery.js but it did'nt work. So, is it possible??
Regards
Prabhjot singh
Comment 51 written by Raymond Camden on 6 April 2009, at 5:00 PM
http://docs.jquery.com/Plugins/Authoring
Comment 52 written by Trevor on 15 April 2009, at 4:30 PM
http://pastie.org/447779
-TR
Comment 53 written by adnan on 8 June 2009, at 11:59 AM
Thanks for sharing your ideas. But like others I also love the Spry validation of dreamweaver because of its ease of use and modifications. I can simple add the spry validation using mouse clicks. And I found a way for using the Spry validation of dreamweaver with jQuery ajax form submit here: http://abcoder.com/javascript/using-dreamweaver-sp...
Comment 54 written by Ben Lopez on 12 June 2009, at 11:01 AM
I could not get the required class to work on a file upload. I was using the recommended mime-type for the accept attribute "image/jpeg". When I changed it to accept="jpg" ... problem solved.
Thanks for posting this tutorial. I look forward in seeing more on jQuery from you.
I'm with you on learning one thing at a time. Unfortunately, for me I have a job offer and they are using DoJo...I understand that the 2 frameworks can will not conflict.
Thanks again Ray.
Comment 55 written by Raymond Camden on 12 June 2009, at 11:11 AM
Dojo: Yep, you can set up jQuery to use another 'root' instead of $. That way it won't conflict.
Comment 56 written by Jay on 20 July 2009, at 2:03 AM
I've had a few stints with the plugin myself -- But I was wondering if you knew of a way to 'highlight' the fieldset/div/table row (or another parent element) instead of adding a label with the error message inside. Know what I mean? So if the name textbox is empty, that entire div is highlighted, instead of the label action.
That's what I'm working on now. Would love your thoughts on it if possible.
Fantastic site btw -- keep up the great work :)
~Jay
Comment 57 written by Raymond Camden on 20 July 2009, at 5:33 AM
http://jquery.bassistance.de/validate/demo/
The demo with 'custom display' is close to what you want I believe.
Comment 58 written by nemrod on 27 July 2009, at 7:52 PM
Comment 59 written by Bill on 27 August 2009, at 7:31 AM
But I am new at this, that said:
Why do you prepend the fields with the letter "c"?
Is it a convention or part of the api for the validation plugin?
It seems to be kind of sporadic to the untrained eye.
I've attached your code below as an example.
<pre>
<p>
<label for="cname">Name</label>
<em>*</em><input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cage">Age</label>
<em>*</em><input id="cage" name="cage" size="4" class="required number" min="1" max="100" />
</p>
</pre>
Thanks!
Comment 60 written by Raymond Camden on 27 August 2009, at 7:34 AM
So... ignore/don't worry about/etc. :)
[Add Comment] [Subscribe to Comments]