Bootstrap jQuery & HTML5 Form Validation Demo
jQuery4u @samdeering
by jquery4u
HTML
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css">
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<form action="#" method="post" id="rego-form" name="rego-form" class="form-horizontal">
<div class="control-group" autocomplete="off">
<label class="control-label" for="name">Full Name</label>
<div class="controls">
<div class="input-prepend"> <span class="add-on"><i class="icon-user"></i></span>
<input type="text" class="input-xlarge" placeholder="Enter your full name" id="name" name="name" data-rule-required="true" />
</div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="email">Email</label>
<div class="controls">
<div class="input-prepend"> <span class="add-on"><i class="icon-envelope"></i></span>
<input type="text" placeholder="Enter your email address" id="email" name="email" data-rule-required="true" />
</div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<div class="input-prepend"> <span class="add-on"><i class="icon-lock"></i></span>
<input type="password" placeholder="Enter your password" id="password" name="password" data-rule-required="true" />
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn">Submit form</button>
</div>
</div>
</form>
JavaScript
$(document).ready(function () {
$('#rego-form').validate();
});
/**
* jQuery Validation Plugin 1.11.0pre
*
* http://bassistance.de/jquery-plugins/jquery-plugin-validation/
* http://docs.jquery.com/Plugins/Validation
*
* Copyright (c) 2012 Jörn Zaefferer
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
(function($) {
$.extend($.fn, {
// http://docs.jquery.com/Plugins/Validation/validate
validate: function( options ) {
// if nothing is selected, return nothing; can't chain anyway
if (!this.length) {
if (options && options.debug && window.console) {
console.warn( "nothing selected, can't validate, returning nothing" );
}
return;
}
// check if a validator for this form was already created
var validator = $.data(this[0], 'validator');
if ( validator ) {
return validator;
}
// Add novalidate tag if HTML5.
this.attr('novalidate', 'novalidate');
validator = new $.validator( options, this[0] );
$.data(this[0], 'validator', validator);
if ( validator.settings.onsubmit ) {
this.validateDelegate( ":submit", "click", function(ev) {
if ( validator.settings.submitHandler ) {
validator.submitButton = ev.target;
}
// allow suppressing validation by adding a cancel class to the submit button
if ( $(ev.target).hasClass('cancel') ) {
validator.cancelSubmit = true;
}
});
// validate the form on submit
this.submit( function( event ) {
if ( validator.settings.debug ) {
// prevent form submit to be able to see console output
event.preventDefault();
}
function handle() {
var hidden;
if ( validator.settings.submitHandler ) {
if (validator.submitButton) {
// insert a hidden input as a replacement for the missing submit button
hidden = $("<input type='hidden'/>").attr("name",...