You may or may not know, but DNAide includes a number of useful javascript library code. Typically, these are based on the brilliant jQuery library.
I have to admit, I'm not a fan of the built in .net 2.0 validation controls. I prefer something cleaner - so I wrote ValidationAide!
Please note, ValidationAide is not .NET 2.0 specific. It is purely client side coding and is not tied to .NET 2.0 development. It just so happens I've bundled it with DNAide. (There is a separate ValidationAide download available).
See the documentation and demos in action here!
Download the source from here.
So what does it do / what is it? Basically, it's a javascript client side form validation script based on jQuery and initially inspired by the great prototype based work found at Dexagogo. Field errors can be grouped and reported together (as an unordered list of hyperlinks to errored fields), or errors can be reported inline. In addition errored fields can have their css modified to reflect an error. You can even hook into pre and post validation events down to the field level to do your own magic!
Integrating client side form validation is as simple as adding a css class to a form element e.g.
<input type="text" id="FirstName" class="text validator-required" />
The above adds a "validator-required" class to the "FirstName" text box. "validator-required" is in fact the id of the validation rule that ValidationAide will check the field value against "on submit". The css class itself does not actually exist on any css style sheet. You can add multiple "validator classes" per field. ValidationAide will check through each one sequentially, returning a field error on the first rule failure.
ValidationAide itself comes with a number of pre-built rules such as required, email address etc. You can of course write your own.
Actions speak louder than words! So please feel free to have a look over the online demos and documentation I've posted here and let me know what you reckon!
For those of a .NET persuasion.
A fair question is how do I use this in my .NET project? Rather than using the .NET validation controls for client an server validation, I prefer to use ValidationAide for relatively simple client side validation e.g. checking mandatory fields are completed, email addresses / dates are of the correct format. Simple stuff. I then have a busness rules / controller object which acts as the project's "bouncer". This checks everything including more complex business rules / logic not handled by ValidationAide. I believe this pattern works very well. More informaiton can be found at http://www.codeplex.com/DNAide/Wiki/View.aspx?title=Validation&referringTitle=Home
Remember, if using ValidationAide in your .NET based website, you do not need to add the ValidationAide js into your website. You can simply use ScriptAide from your code behind e.g.
ScriptAide.CurrentContext.RegisterDNAideScriptResource(DNAideScriptResource.JQueryValidationAide);
Monday, 28 May 2007
Subscribe to:
Post Comments (Atom)
74 comments:
Hi,
I was wondering if its possible to use the jQuery forms ajaxSubmit() method to handle the submit. This look like an awesome plugin, great work!
Hi Hagan, thanks for the comment, really appreciated :D
I haven't used the ajaxSubmit() method before. Is this the plugin found at be.twixt.us?
I guess you could tie this in to any other plugin by simply calling at the appropriate point:
jQuery.validationAide.validateForm(formId, validationRules, options, preFieldValidation, postFieldValidation);
I'll look into this a bit more. I'd be interested to know how you get on and more than willing to extend the plugin to include this.
Is posible this script in other languages? in spanish.
Or
class="textbox validator-required"
sustitute for
class="textbox validator-required:campo obligatorio"
Excuseme, my english isnt good
Heya
I am submitting a form from a button. I have added an onclick event handler to the button which submits the form but it doesn't validate first. Any clues how I might be able to do this?
Cheers
Done it. No worries.
Hi Amy, please let me know if you have any other issues. Thanks for trying it out :D
Hi,
Very good job !
I just want to report you that "validator-email" doesn't match strict "email pattern".
For example ("ee _ eee@yahoo.com"), you can enter a space character in the field, and that doesn't say that email is not valid.
Is it possible to use your code in commercial web application ?
CDDL allows that ?
Sorry for my bad english too ;-)
Regards,
Fabien
Hi Fabien, thanks for spotting the flaw in the email validitor reg ex, I'll look into updating this for the next version. In the meantime, you can get around this by creating your own email rule.
Yes you can you this software:
"You may create a Larger Work by combining Covered Software
with other code not governed by the terms of this License
and distribute the Larger Work as a single product. In such
a case, You must make sure the requirements of this License
are fulfilled for the Covered Software. "
Thanks for trying thins and your very useful comments :D
Hi again,
I've got two inputs with text validator classes inside which I've put inside a table i.e.
<table>
<tr>
<td>
<input type="text" class="textbox validator-required"/>
</td>
<td>
<input type="text" class="textbox validator-url"/>
</td>
</tr>
</table>
For some reason the second input does not get validated. I think it might be because it is in a table?
Perhaps you could help me?
Thanks, in advance.
Amy
Hi Amy,
I've just tried the validator in a table and all seems fine.
Couple of things I spotted:
- the "validator-url" rule only fires if a value if entered. So if you want to enforce this field to be mandatory, you have to add the "validator-required" class as well e.g. "validator-required validator-url"
- I recommend providing each input with an id attribute.
<table>
<tr>
<td><input type="text" id="FirstName" title="First name" class="validator-required" /></td>
</tr>
<tr>
<td><input type="text" id="Url" title="Url" class="validator-required validator-url" /></td>
</tr>
</table>
Hope this solves your problem! Thanks.
Hi Ste
Thank you so much for your help. As you suggested adding class="textbox validator-required validator-url" worked.
Once again, thank you for saving me time!
Cheers
Amy
Hello
can I have custom error messages for each type of validation and for a specific input?
Thanks a lot
Will this work with selects?
Hi jailjack, this has now been added to the new version and I've also created a page demoing this
Hi eric, yes ValidationAide works with selects.
<select id="SelectTest" title="Select Test" class="validator-required">
<option value="">Please select</option>
<option value="1">1</option>
</select>
I am using this on my "post" and "edit post" pages of my wordpress backend to make all fields required. It works great except for one thing, on my edit pages I also have a "delete post" button, if the fields are empty i get the message for the required fields when i am really just trying to delete the post. I guess my question is... Is there a way to make one button escape the validation?
Sure, I work on a lot of .NET projects and this is needed quite a bit.
Try enabling and disabling on individual buttons e.g.
$("#SubmitBtn").validationAideEnableOnClick("#MyForm");
$("#CancelBtn").validationAideDisableOnClick("#MyForm");
Hope this helps.
does this work for multiple forms on the same page? i want to have the ClientValidationSummary show up in different places based on which form was sumitted on a page...
Create script. Was wondering how I would remove the Validation Summary upon clicking a cancel button. Tried validationAideDisableOnClick but the message and colored inputs are still there. Trying to use this in a modal window; the user clicks submit, gets validation errors, clicks cancel, opens the modal again they see the old validation errors.
Dont know if you noticed but your Date validation on mm/dd/yyyy doesnt work. 12/12/2007 passes but 08/11/2007 doesnt.
Hi Emmet, yeah we have noticed this and it's been fixed in a forthcoming release. Many thanks!
Hi Paul, yes this script should work fine on multiple forms. Should just be $("#Form1").validationAideEnable() / ("#Form2").validationAideEnable()
I am currently in the process of finalizing a new release and will double check this out / add to the examples.
Thanks
Hi Thanks
Validationaide is amazing.
Today I was implementing a TinyMCE in textarea and didnt work.
body class="mceContentBody text validator-required" dir="ltr" spellcheck="false"
Another strange behavior is when I click in a upload button of my for Validationaide think that I am trying submit my form and try validate. The problem is after this my ajax upload button stop work. Always Hacking. But anyway thanks and congratulation.
Regards
Mario
Hello,
I was using your validation plugin and it is amazing, it reduced the previous code from 500 lines to just adding classes. Thank you,
However, I was trying to tweak it myself (but I couldn't get it) to add in password checking against the verify password field and also checking a valid phone number. If you have any ideas or code for me that would be great.
Timothy Haroutunian
- tgharoutunian@gmail.com
It is a good plugin, maybe you need to give more flexibility with parameters inline or error display.
You can get some "inspiration" of jQuery Validation plugin (http://jquery.com/plugins/project/validate) or jQuery.YAV plugin (http://jquery.com/plugins/project/jquery_yav)
Also you can optimize your code for the HTML generation setting the < a > onclick event with jQuery DOM methods:
jQuery("<_a>").html("some text").onclick(function(){ //code)}).appendTo("#someID");
Anyway, good work! :)
Thanks for you comments Sevir.
The new version of ValidationAide includes the ability to supply your own inline error message string format.
Yeah, I had a look over the official validate plugin and it's very comprehensive. I really wrote this as a very lightweight way of adding simple client validation code with as minimum effort as poss.
I'll be releasing the new version soon (it'll form part of the new DNAide)
Thanks again!
Hi Timothy, you should be able to achieve this by creating your own custom rules. The demo code includes examples of doing this.
Hope this helps!
Thank you, that helped. Can I validate select boxes as well (I couldn't find anything about it).
Hi Timothy, yeah you can validate select boxes and other form field types - simply add the rule to you field e.g.
<select id='MySelect' class='validator-required'>
<option value="">Please select</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>
This field would fail vaildation unless either option 1 or 2 has been selected.
I've watched the demo, very impressive, I want to use it, is it possible use your Plugin with the jQuery Form plugin: http://www.malsup.com/jquery/form/, if yes how? Many thanks.
Thanks, I had that, but I forgot to put an ID on the select box :)
You rule!!
GREAT script - many thanks. Just wondered if there was any way to display the form results to a div on the same page, basically incorporating the form validation into this function:
function send(){
var params = Form.serialize($('processform'));
new Ajax.Updater('formdiv', 'help.php', {asynchronous:true,parameters:params});
}
input type="button" onClick="send();" value="Submit"
Thanks again!
I think the "ajaxSubmint" meant this: http://www.malsup.com/jquery/form/ .
It would be great if these two excellent plugins works together.
xSVUzX
TTStQz Nice Article.
DQO2Cl Thanks to author.
H1LHLo actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.
BhRK3h Wonderful blog.
aPHQGn Thanks to author.
Good job!
Magnific!
Thanks to author.
Thanks to author.
Nice Article.
Good job!
Good job!
Nice Article.
KLGYsu You have a talant! Write more!
UHgNVw Good job!
guvxhN Magnific!
Wonderful blog.
OFFTtx The best blog you have!
F1JNyx Magnific!
Magnific!
Magnific!
Thanks to author.
Please write anything else!
Please write anything else!
Nice Article.
actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.
Wonderful blog.
nBIYdS write more, thanks.
Nice Article.
actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.
Please write anything else!
actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.
Magnific!
Hello all!
Thanks to author.
Thanks to author.
Magnific!
Nice Article.
Good job!
Post a Comment