HTML 5 Form Validation with Yepnope Fallback
In my last post about creating a proper fallback when using the new HTML 5 form validation I mentioned using modernizr to detect for support, then creating an if statement to call the jQuery validate plugin.
One of the commentors mentioned using yepnope for this instead of loading the jQuery validation plugin by default. Since modernizr has yepnope now anyway, I thought it was a good idea. And here we are with some ne code to look at.
Dependencies
The HTML
<form action="" method="post">
<ul>
<li>
<label for="name">Name</label>
<input type="text" name="name" id="name" required>
</li>
<li>
<label for="email">Email</label>
<input type="email" name="email" id="email" required>
</li>
<li>
<button type="submit">Submit</button>
</li>
</ul>
</form>The JS
(function ($) {
// set and cache some variables, change var forms to whatever forms you want to validate
var forms = $('form'),
formsCount = forms.length,
items = forms.find('input[required]'),
count = items.length;
// check for "required" input support with modernizr
if (Modernizr.input.required) {
// do something else, maybe customize the output messages?
} else {
// loop though each required input and set the required and type class jQuery validate needs
for (var i = 0; i < count; i += 1) {
var obj = items[i];
$(obj).attr('class', 'required ' + obj.getAttribute('type')).removeAttr('required');
};
// after the classes are set, load in the plugin with yepnope , you can do this with Modernizr.load as well
yepnope([{
load: 'js/validate.js',
complete: function () {
// after the plugin is loaded, call the method on each form listed at the top
for (var i = 0; i < formsCount; i += 1) {
$(forms[i]).validate();
}
}
}]);
} // if required supported
}(jQuery));
I’m not really a fan of script loaders, especially when using them to load a core script file, like jQuery, but I think this is actually a pretty good use of yepnope.
I don’t know about you folk(s), but I’ll be using this moving forward. Any comments/corrections are obviously welcome.
Both comments and pings are currently closed.
« Required input fields with JS fallback | Creating a Unique and Scalable Mobile Experience »
|
Comments (5)
|
It’s nice to see this code finally laid out somewhere. Thanks!
Agreed, I’ve known about this for a while but hadn’t really seen anyone code it up and put it out there. Looks easier than I thought
I have a heart on for yepnope
One of the great things about it is has a built in ‘test’ property, so you can do all of your feature detection and conditional behavior inside the yepnope object. I put up a gist with an example based on your code:
https://gist.github.com/6675156d7ebf93569734
is there any real difference in using the yepnope vs moderizr.load for something like this?
This code works almost perfectly, i just changed this to keep my classes on inputs :
$(obj).attr(‘class’, obj.getAttribute(‘class’)+’ required ‘ + obj.getAttribute(‘type’)).removeAttr(‘required’);