Show HN: a small jQuery Form Validation library
github.com
github.com
[1] http://afarkas.github.com/webshim/demos/demos/webforms.html
[2] https://developer.mozilla.org/en-US/docs/HTML/HTML5/Constrai...
The "min_length(6)|required" isn't actual javascript code is it?
Would passing an object be better?
For example,
userForm.addValidation("username", "min_length(6)|required");
Becomes,
userForm.addValidation("username", {min_length:6, required:true});
Did I misunderstand something?
Thank you so much for this. I've no idea why I didn't think of this initially - but I've now rewritten the plugin to do this instead. Hope you like it - and thanks again!
Jack
Thanks!
Everytime someone releases a library that is jQuery only, you've reduced the utility of that library in a lot of use cases. Not only that, as awesome as jQuery was, we're now entering an age where it's better to have separate libraries for each task (e.g. a library that only does XHR and does it well like TJH's SuperAgent) instead of one large slow moving monolithic library.
NPM as a package manager has done so much to make the javascript community vibrant on the server-side and we need to see more of the many solid single-purpose libraries on the client-side too.
Can you expand on this? I was hoping to implement a custom valildation library as I am also unhappy with $.validate(). Any links would be helpful, too.
Then for validation, you should create a validation with interfaces/apis similar to something like underscore.string.js. It should basically validate the content of form inputs, which will usually be strings and numbers. These functions should know absolutely nothing about the DOM or client-side javascript. They should only know about the data you are interested in.
Building your validation library like this means it can be used in at least three different ways: (1) use like the library being discussed in this thread (2) use at the model layer on the client-side with a framework like backbone.js or angular.js for examples (3) use on the server side if you are using something like node.js that allows javascript execution.
Now once you have that general purpose data validation library, you can achieve use (1) by coupling it with a tiny adapter library via the module system of your choice. In this adapter library you require(your_validation_library) as a local var and then create a chainable jQuery interface that takes the `this` variable which is usually bound to the element currently being processed when creating a jquery plugin and pipes the val() of that element to the appropriate function in your validation library.
One way to do this is to have a one to one mapping between some DOM attribute value (like a classname or a custom data attribute like data-validation) and the functions in your validation library. With this approach, you get that attribute value from `this` and use a hashmap to determine what validation function (or functions if your hash map contsains arrays of function names as the value in the k-v pairs) is appropriate.
I also like the idea of using HTML5 attributes (see this[1] tutorial for a starting point). I could easily see a place for a "add html5 validation to every browser" where your plugin provides the backwards compatibility. In fact, I might prefer to be able to use standard markup but not have to worry that I'll get different behavior on different browsers.
1. http://www.the-art-of-web.com/html/html5-form-validation/
$("ul").html("");
can be replaced with $("ul").empty();
The replacePlaceholdersInMessage function uses replace with string as the match, which means that in rare cases where more than one %F is present, for instance, only the first will be replaced. To fix this you have to pass in a new RegExp object with the 'g' (global) flag instead. See https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...Overall, good job! Lastly, I'd also suggest allowing option passing via $.extend.
It would also be much easier to use, because there would be no API to "learn".
[1]: http://www.matiasmancini.com.ar/jquery-plugin-ajax-form-vali...
Give it a look. As you described, it simply parses standard input tag params, including HTML 5 types. It's my first attempt at anything like this, so be gentle.
How do other developers solve this problem?
I've found a very practical solution on the .NET side of things is to use data annotations with MVC - which allows you to markup your model with metadata describing the shape of acceptable data and have it automagically validated on both client and server - but haven't found anything similar in PHP and other languages. Any recommendations?
It does basically what you describe, but once you combine it with some other gems and add in some non-standard validations, it seems to get a bit unwieldy. I'm newish to rails but it makes me pine for Node.js, where I could literally just run the same validations on the client and server. Do any rails gurus know if this is best practice?
And what are the "non-standard validations"? If you are referring to custom validations that you wrote then you'll have to write the supporting javascript validation.
As waxjar says I think it would make more sense to use attributes from the field itself.