Our web forms are really complex. What's a great solution for extensible form validation, preferably one that works with jQuery?
Background:
Our site has a bit of Ajax, but the real focus is on user experience through about 20 multi-page forms or "wizards." These forms are complicated.
5
into a price field, the field is updated to 5.00
.onSubmit
— we sometimes post multiple forms in order from the same page using Ajax. (E.g., we let users sign up and create a widget in one swoop but, due to legacy systems, that flow requires two POST requests.) We're currently using the jQuery Validation library but our forms appear to be outgrowing its capability. I've been looking at things like <angular/>, Knockout and Backbone.js, but I'm worried that they're too heavyweight or that they would require us to rewrite our frontend.
(This should be a community wiki.)
Scripting languages such as JavaScript and VBScript are used for client-side validation. In this kind of validation, all the user input validation is done in user's browser only.
Notice that for validation, the JavaScript function containing the code to validate is called on the onSubmit event of the form.
JavaScript FormsHTML form validation can be done by JavaScript.
Client-side validation is executed by the client and can be easily bypassed. Client-side validation is a major design problem when it appears in web applications. It places trust in the browser, an entity that should never be trusted.
This is a shameless plug, but might I volunteer a framework that I designed? I've built it based on annotations (a la Hibernate Validator). It has support for custom constraints and I feel that it is pretty powerful. Here is also a Stackoverflow question where I asked for a review of the framework.
5
to 5.00
).Here are a few examples:
The following shows standard validation, with built-in constraints:
<input id = "myInput" name = "myInput" type = "text" class = "regula-validation" data-constraints = '@NotEmpty @IsNumeric @Between(min=1, max=5)' /> jQuery(document).ready(function() { // must call regula.binnd() first. The best place would be in an // onload handler. This function looks for elements with // a class name of "regula-validation" and binds the // appropriate constraints to the elements regula.bind(); jQuery("#myForm").submit(function() { // this function performs the actual validation var validationResults = regula.validate(); for(var index in validationResults) { var validationResult = validationResults[index]; alert(validationResult.message); } }); });
As you can see, you're only working with constraint violations, and so the manner in which you display the error message is entirely up to you.
Here's an example of a custom constraint:
regula.custom({ name: "MustBe42", defaultMessage: "The answer must be equal to 42", validator: function() { return this.value == 42; } });
And its use:
<input id = "theAnswerToLifeTheUniverseAndEverything" name = "theAnswerToLifeTheUniverseAndEverything" value = "" class = "regula-validation" data-constraints = "@MustBe42" />
Since the validator is a Javascript function, you can have it do anything (so this addresses your question about side effects).
Here is an example of another constraint that accepts parameters:
regula.custom({ name: "DivisibleBy", defaultMessage: "{label} must be divisible by {divisor}", params: ["divisor"], validator: function(params) { var divisor = params["divisor"]; return (this.value % divisor) == 0; } });
And usage:
<input id = "number" name = "number" value = "" class = "regula-validation" data-constraints = "@DivisibleBy(divisor=3, label='The Number')" />
Here is an example of using validation groups:
<input id = "score" name = "score" type = "text" class = "regula-validation" data-constraints = '@IsNumeric(label="Score", message="{label} needs to be a number!" groups=[FirstGroup, SecondGroup, ThirdGroup]' /> <input id = "age" name = "age" type = "text" class = "regula-validation" data-constraints = '@IsNumeric(label="Age", message="{label} needs to be a number!" groups=[SecondGroup]' /> <input id = "name" name = "name" type = "text" class = "regula-validation" data-constraints = '@NotEmpty(label="Name", message="{label} cannot be empty!" groups=[FirstGroup]' />
And a snippet that validates only FirstGroup
(so only score
and name
are validated):
var constraintViolations = regula.validate({groups: [regula.Group.FirstGroup]}); var messages = ""; for(var index in constraintViolations) { var constraintViolation = constraintViolations[index]; messages += constraintViolation.message + "\n"; } if(messages != "") { alert(messages); }
If you're planning on trying it out, I recommend downloading version 1.1.1. The current documentation matches that version specifically. In 1.2.1 I added support for compound constraints, but I haven't updated my documentation to reflect that.
I understand if this doesn't address all your concerns, or if this is not what you are looking for. I thought I'd just put it out there. Also, if you do check it out then I will make sure to update the documentation to reflect version 1.2.1. I've been busy with school and work and so I haven't had the time to do that.
UPDATE #1
Sohnee mentioned client-side validation. I'm actually working on an integration between Regula and Spring 3. Hopefully I should be able to release it sometime soon (depending again, on work and school). The integration works by translating Hibernate validation-constraints to Regula validation-constraints. This way, you only have to write validation code once (mostly). For custom constraints though, you will still have to write code on the Javascript side (the custom validator). But once you annotate code on the server-side with Hibernate validation-constraints, you don't need to do anything on the client-side; those constraints automatically get applied to form elements on the client-side.
Matthew Abbott has also been able to integrate Regula with ASP.NET MVC.
UPDATE #2
I've got a demo webapp (mavenized) on github that showcases the integration between Regula and Spring 3.0.x Web MVC using Hibernate Validator. It's not really documented or anything, it's more proof-of-concept. I plan to add some documentation to the github page about the integration and how it works.
UPDATE #3
I've updated the documentation on the wiki, and it now corresponds to the latest version, 1.2.2 (I made a little bugfix, which is why it is 1.2.2 now).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With