Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've tried out two javascript form validation frameworks - jQuery validation, and jQuery Tools validator - and I've found both of them lacking. jQuery validation lacks the clear separation between the concepts of "validating" and "displaying validation errors", and is highly inflexible when it comes to displaying dynamic error messages. jQuery Tools on the other hand lacks decent remote validation support (to check if a username exists for example). Even though jQuery validation supports remote validation, the built-in method requires the server to respond in a particular format. In both cases, any sort of asynchronous validation is a pain, as is defining rules for dependencies between multiple inputs.

I'm thinking of rolling my own framework to address these shortcomings, but first I want to ask... have others experienced similar annoyances with javascript validation? What did you end up doing? What are some common validation requirements you've had which really should be catered for? And are there other, much better frameworks out there which I've missed?

I'm looking primarily at jQuery-based frameworks, though well-implemented frameworks built on other libraries can still provide some useful ideas.

share|improve this question
    
I completely misread your question. I now realise that you are talking about input validation, rather than validation of code. –  eBusiness Dec 13 '10 at 1:24
    
@eBusiness - thanks, I'll update the question to say "form validation" –  box9 Dec 13 '10 at 1:46
    
Consider using HTML5 validation or building a shim for browser support. –  Raynos Jul 24 '11 at 10:44
add comment

closed as not constructive by Jim G., gnat, Walter, GlenH7, Thomas Owens Oct 24 '12 at 14:27

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

6 Answers

This is one of those tasks where interfacing with a framework easily takes as long time as doing it yourself. Don't write a framework, just solve your own problem and let other people do the same.

If a validation framework is to be of any real value I'd say it would have to be built into a user management and login system.

share|improve this answer
1  
+1 for proper advocation of "rolling your own." this is one of the tasks that depending on the complexity of policies/requirements its faster to DIY than try to adapt a framework and/or plugin. –  jellyfishtree Dec 13 '10 at 1:37
add comment

Form validation is easy. use the HTML5 API and the Shim for browser support.

share|improve this answer
1  
Unfortunately HTML5 form validation is extremely basic. There's no support for server-side validation or dependencies for example. –  box9 Jul 24 '11 at 11:56
    
@box9 how does jQuery validation do server-side validation? And "dependencies" is a very vague non-descriptive word to use here. –  Raynos Jul 24 '11 at 12:11
    
sorry - by dependencies I mean for example if the valid set of inputs in one field depends on the value in another - the typical example is the "repeat password" field, but that is a simple one. jQuery validation has the remote() method for server-side validation. –  box9 Jul 24 '11 at 23:37
add comment

For me the thing that's really missing (beyond what's already been mentioned) is a good clean way to define validations. Most frameworks go one of two ways: they make you define all the validations in meta-markup (i.e. class names, data attributes, etc.) OR you define all the validations in complicated JS options structures (i.e. big blog of JSON). It seems that neither approach is particularly good. On the one hand, the markup route allows you to be resilient to change somewhat, but it leaves you with really littered markup. On the other side, you might have a clean JS object that defines your validations but if the markup changes you have a synchronization issue.

share|improve this answer
add comment

I too am getting tired of the amount of time that gets spent on getting these things running only to find out that it doesn't have the ability to do something pretty basic and fundamental that you thought it would. This may be the fault of the browser too somewhat.

For example, a common problem is when the user selects from the autocomplete list that the browser gives you when you click on the form field, it sometimes doesn't behave right -- you have to actually type in the data to get the validator to validate it.

share|improve this answer
add comment

I found jQuery Validate to be pretty good. You can specify you error messages as functions which I think gives you pretty decent separation. I think that writing a clever message generator would solve your problem without forcing you to start from scratch.

share|improve this answer
add comment

You can take a look at regula. It's a form-validation framework that I've created and it is based on constraints. IMHO it's pretty powerful and better than the programmatic-approach offered by jQuery validator and the like. I'm currently in the process of adding support for HTML5 validation attributes if the browser supports it. I'm probably won't be performing HTML5 validation emulation because of the difficulties and inconsistencies for input types like date, month, datetime etc.

I have a detailed answer on StackOverflow that describes some of the features, and the github page also has information about the framework as well as documentation.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.