So you want to use jQuery validation, huh?
What is it? Something that was added to the holy jquery site and is an easy way to validate input from users. Now this should in no way take over for server side validation, but it helps to at least catch a few things without having to send anything to the server. So how do ya do it?
Well to start, you need some files:
jquery-1.3.2.js and jquery.validate.js.
Now oddly enough the validation file isn’t hosted on the holy jquery site but how to use it is.
Ok now you have the files, what’s next? Well you need form, and I can do that for you.
So basically it’s a simple form with one input that is required.
jQuery(document).ready
(
function()
{
jQuery("#PrimaryForm").validate
(
{
errorLabelContainer: "#ErrorDiv",
wrapper: "div",
rules:
{
FirstName :
{
required : true
}
},
messages:
{
FirstName:
{
required : 'First Name is required.'
}
},
onfocusout : false,
onkeyup: false,
submitHandler: function(label)
{
postSubmit();
}
}
);
}
jQuery("#PrimaryForm").validate
Real simple, just setting the validator to the primary form on the page.
errorLabelContainer: "#ErrorDiv",
This sets the errors to show up in the ErrorDiv. Now this is optional, as you can have it show the errors next to the FirstName text box but personally I think that looks horrible. Setting up the ErrorDiv puts all the errors in one central location and allows for styling the actual div.
rules:
{
FirstName :
{
required : true
}
},
This matches an element with the id of FirstName to the required rule, meaning that FirstName is required. Rocket science.
messages:
{
FirstName:
{
required : 'First Name is required.'
}
},
If you can’t figure this out, I hear circus is hiring for the “World’s Dumbest Person”. You’ll fit in with Jub Jub the Dog Boy.
onfocusout : false,
onkeyup: false,
Basically this prevents the validation when leaving the textbox or on every key press. This is just another preference.
submitHandler: function(label)
{
postSubmit();
}
If the submit is successful, call this method.
But… BUT WHAT IF IT’S AN EMAIL?!??! WHAT WILL I DO???!?!?
Well for one, stop being such a child. And two, look here.
Some what different, as you can see it’s now email and there is one extra requirement in the rules:
rules:
{
EmailAddress :
{
email : true,
required : true
}
},
messages:
{
EmailAddress:
{
required : 'Yo, email fool.',
email : 'So not an email address.'
},
},
See? It has nice built in rule for email. Simple.
BUT WHAT IF I NEED A REGULAR EXPRESSION?!??! WHAT WILL I DO???!?!?
I swear if you don’t stop that, I’m turning this post around and going home.
Fine here it is.
jQuery.validator.addMethod
(
"isZipCode",
function(value, element)
{
return value.match(new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/));
}
);
Just have to create a method and “add it” to the validator itself. And then there’s the use:
rules:
{
ZipCode :
{
required : true,
isZipCode : true
}
},
messages:
{
ZipCode:
{
required : 'For the love of me, enter a zip code!.',
isZipCode : 'Serioulsy? Do you know what a zip code is?'
},
},
Woo hoo right?
Don’t do it… Don’t you yell.
But what if one input depends on another?
Much better. Well that’s not as hard as it may seem and here’s the example.
rules:
{
InputB :
{
required :
{
depends : function(element) { return jQuery('#InputA').val() != "" }
}
}
},
As you can see, you can change how the required method works by adding in a depends handler. Works out pretty well.
Yes I will show you how to make sure two inputs match. I swear you ask for a lot.
rules:
{
Password :
{
equalTo : "#ConfirmPassword"
},
},
Couldn’t be easier unless I wrote it out for you. Wait, I did.
So here you’ve either learned a bit about jQuery validation or have just spent the last few minutes drooling uncontrollably. Either way, I’m done with this post and you’re left to do whatever it is you do, you sick f—.
Side note: I haven’t actually been to Htmlgoodies since eh college? but wow did that place sell out. How fitting that an introduction to html page now looks like it was designed by someone just starting out… in the 90s.