Monday, 28 May 2007

ValidationAide - "Easy as" javascript client side form validation!

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);

74 comments:

Hagen said...

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!

Ste Brennan said...

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.

PoZu said...

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

Amy said...

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

Amy said...

Done it. No worries.

Ste Brennan said...

Hi Amy, please let me know if you have any other issues. Thanks for trying it out :D

C.F. said...

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

Ste Brennan said...

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

Amy said...

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

Ste Brennan said...

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.

Amy said...

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

JaliJack said...

Hello
can I have custom error messages for each type of validation and for a specific input?
Thanks a lot

Eric said...

Will this work with selects?

Ste Brennan said...

Hi jailjack, this has now been added to the new version and I've also created a page demoing this

Ste Brennan said...

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>

The Design Coalition said...

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?

Ste Brennan said...

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.

paul said...

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...

Anonymous said...

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.

Emmet said...

Dont know if you noticed but your Date validation on mm/dd/yyyy doesnt work. 12/12/2007 passes but 08/11/2007 doesnt.

Ste Brennan said...

Hi Emmet, yeah we have noticed this and it's been fixed in a forthcoming release. Many thanks!

Ste Brennan said...

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

Mario Moura said...

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

Timothy said...

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

SeViR said...

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! :)

Ste Brennan said...

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!

Ste Brennan said...

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!

Timothy said...

Thank you, that helped. Can I validate select boxes as well (I couldn't find anything about it).

Ste Brennan said...

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.

Anonymous said...

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.

Timothy said...

Thanks, I had that, but I forgot to put an ID on the select box :)

You rule!!

Anonymous said...

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!

deerchao said...

I think the "ajaxSubmint" meant this: http://www.malsup.com/jquery/form/ .

It would be great if these two excellent plugins works together.

<a href="http://medonlineshops.com">OnlinePharmacy</a> said...
This post has been removed by a blog administrator.
name said...

xSVUzX

<a href="http://m1.aol.com/MacBoyd45/index.html">a good screw with viagra</a> said...

TTStQz Nice Article.

<a href="http://m1.aol.com/CoryDyer55/index25.html">buy eon phentermine hydrocodone line buy phenter</a> said...

DQO2Cl Thanks to author.

name said...

H1LHLo actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

<a href="http://m1.aol.com/IvySalas33/104_261007.html">meridia vs phentermine</a> said...

BhRK3h Wonderful blog.

<a href="http://members.ospa.us/portal_memberdata/portraits/tddddfelw">columia sc motels and hotels&lt;</a> said...

aPHQGn Thanks to author.

name said...

Good job!

name said...

Magnific!

name said...

Thanks to author.

<a href="http://pasados.t35.com/index1.html">brian jacques us tour 2007</a> said...

Thanks to author.

<a href="http://users2.TitanicHost.com/pakurito/index6.html">concert def leppard tour</a> said...

Nice Article.

<a href="http://www.optimising.biz/portal_memberdata/portraits/tsdpiwhaz">travel insurance select</a> said...

Good job!

<a href="http://learning.hsc.hccs.edu/portal_memberdata/portraits/tnglpmobm">ringtones</a> said...

Good job!

<a href="http://www.bcrobotics.org/portal_memberdata/portraits/tunaqpwhm">Money to loan classifieds&lt;</a> said...

Nice Article.

<a href="http://paydayadvisors.org">PaydayLoans</a> said...

KLGYsu You have a talant! Write more!

<a href="http://tes.uab.es/MISS/portal_memberdata/portraits/twkgxziok">Auto insurance company</a> said...

UHgNVw Good job!

<a href="http://m1.aol.com/EloyRowe59/86-291007.html">cialis drug generic generic generic online viagr</a> said...

guvxhN Magnific!

<a href="http://freeringtones.99k.org/free-ringtones-phone-zune-.html">free ringtones phone zune</a> said...

Wonderful blog.

<a href="http://hydrocodone.99k.org/index.php">Hydrocodone</a> said...

OFFTtx The best blog you have!

<a href="http://users2.titanichost.com/buyviagra/241.html">self insurance teen</a> said...

F1JNyx Magnific!

<a href="http://free.7host07.com/nmcfgy/419.html">alutian islands natural history tour</a> said...

Magnific!

<a href="http://fioricet.clanteam.com/?pharma=173">dr atkins diet what is fioricet</a> said...

Magnific!

<a href="http://celebrex.zxq.net/?pharma=1658">new jersey vioxx celebrex</a> said...

Thanks to author.

<a href="http://m1.aol.com/BrettHead14/345.html">alternative health insurance services</a> said...

Please write anything else!

<a href="http://users2.titanichost.com/buyviagra/index6.html">buy phentermine tablets buy phentermine</a> said...

Please write anything else!

<a href="http://tramadol.newsit.es/cheap-tramadol-very.html">cheap tramadol very</a> said...

Nice Article.

<a href="http://tramadol.newsit.es/no-perscription-tramadol.html">no perscription tramadol</a> said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

<a href="http://phentermine.whdot.com/index19.html">st louis phentermine</a> said...

Wonderful blog.

<a href="users2.titanichost.com/amalopra">JohnBraun</a> said...

nBIYdS write more, thanks.

<a href="http://users2.titanichost.com/popebatret/index.html">coed interracial sex</a> said...

Nice Article.

<a href="http://users2.titanichost.com/inoryum/index19.html">drawn sex passwprd</a> said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

<a href="http://users2.titanichost.com/hviler/index31.html">anime bleach sex</a> said...

Please write anything else!

<a href="http://users2.titanichost.com/ansfur/index13.html">learning anal sex</a> said...

actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

<a href="http://users2.titanichost.com/olds77/index7.html">indonesian anal sex</a> said...

Magnific!

<a href="http://users2.titanichost.com/shingro/index1.html">set of sex</a> said...

Hello all!

<a href="http://users2.titanichost.com/scersi/index.html">dwarf lesbian sex</a> said...

Thanks to author.

<a href="http://users2.titanichost.com/t1fielde/index13.html">oung teen sex</a> said...

Thanks to author.

<a href="http://users2.titanichost.com/adjutes/index5.html">basic sex positions</a> said...

Magnific!

<a href="http://users2.titanichost.com/adjutes/index9.html">beast sex cartoons</a> said...

Nice Article.

<a href="http://users2.titanichost.com/shiconta/index28.html">daddy girl sex</a> said...

Good job!