Created
November 6, 2012 04:40
-
-
Save goldsky/4022619 to your computer and use it in GitHub Desktop.
A javascript function using jQuery's validation and twitter bootstrap's popover
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function validateForm(formId, erPlacement, validRules) { | |
erPlacement = erPlacement || 'bottom'; | |
validRules = validRules || {}; | |
/* Validation message with the twitter's popover */ | |
/* http://d.hatena.ne.jp/hipsrinoky/20120329/1333035926 */ | |
var eClass = 'error'; // for bootstrap | |
var sClass = 'success'; // for bootstrap | |
var wClass = 'warning'; // for bootstrap (not in used) | |
$(formId).validate({ | |
errorElement:"span", //not required. | |
errorClass:"help-inline", //result is <span class="help-inline">xxx</span> | |
validClass:"help-inline", //not required. | |
rules: validRules, | |
unhighlight: function(element, errorClass, validClass) { | |
if (element.type === 'radio') { | |
this.findByName(element.name).parent("div").parent("div").removeClass(eClass).addClass(sClass); | |
} else { | |
$(element).parent("div").parent("div").removeClass(eClass).addClass(sClass); | |
$(element).popover('hide'); //hide popover. | |
} | |
}, | |
highlight: function(element, errorClass, validClass) { | |
if (element.type === 'radio') { | |
this.findByName(element.name).parent("div").parent("div").addClass(eClass).removeClass(sClass); | |
} else { | |
$(element).parent("div").parent("div").addClass(eClass).removeClass(sClass); | |
} | |
}, | |
errorPlacement: function(error, element) { | |
// For invalid messages in popover. | |
$(element).attr('rel','popover'); | |
var erMsg = $(element).attr('data-content') || error.html(); | |
$(element).attr('data-content', erMsg); | |
$(element).popover({ | |
placement: erPlacement, | |
offset: 20, | |
trigger: 'manual' | |
}); | |
$(element).popover('show'); //Show by manual. | |
}, | |
onfocusout: function() { | |
$(this.currentElements).popover('hide') | |
}, | |
onkeyup: false, | |
onclick: false, | |
onsubmit: true | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment