Trying to insert code before the containing div of a $(#FieldID) selected field
(PLEASE JUST READ "SIMPLIFIED QUESTION")
You may be familiar with it from a couple of other posts I have made, it is a jQuery/Bootstrap2 based javascript for validating fields in a form. You can pass a message to display on errors, a regex to use for validation, the div class of the field's container, the Bootstrap2 message class (usually "help-inline" or "help-block"), and a boolean to get the validator only to check for errors upon form submission (instead of "live").
I currently have to disable the "live" option, so it doesn't end up looking like this (repeating error message) if the user keeps entering invalid info, but I would prefer to have it functioning the way it was designed to.
I have been using this site (browz.in) to play around with the code and try to figure out how to get it working properly - ignore the other stuff on there, I'm just using it as my testing ground for now.
Here is the code. What i have done, and what I want it to do, follows ...
/**
* jquery.validate.js for js Bootstrap form validation
*
* @author GeekTantra
* @date 20 September 2009
* http://www.geektantra.com/2009/09/jquery-live-form-validation/
* Modified by dsmith: added support for twitter boostrap
*/
(function(jQuery){
var ValidationErrors = new Array();
jQuery.fn.validate = function(options){
options = jQuery.extend({
expression: "return true;",
message: "",
error_message_class: "help-inline",
error_container_class: "control-group",
live: true
}, options);
var SelfID = jQuery(this).attr("id");
var unix_time = new Date();
unix_time = parseInt(unix_time.getTime() / 1000);
if (!jQuery(this).parents('form:first').attr("id")) {
jQuery(this).parents('form:first').attr("id", "Form_" + unix_time);
}
var FormID = jQuery(this).parents('form:first').attr("id");
if (!((typeof(ValidationErrors[FormID]) == 'object') && (ValidationErrors[FormID] instanceof Array))) {
ValidationErrors[FormID] = new Array();
}
if (options['live']) {
if (jQuery(this).find('input').length > 0) {
jQuery(this).find('input').bind('blur', function(){
if (validate_field("#" + SelfID, options)) {
if (options.callback_success)
options.callback_success(this);
}
else {
if (options.callback_failure)
options.callback_failure(this);
}
});
jQuery(this).find('input').bind('focus keypress click', function(){
jQuery("#" + SelfID).next('.' + options['error_message_class']).remove();
jQuery("#" + SelfID).parents("." + options['error_container_class']).removeClass('error');
});
}
else {
jQuery(this).bind('blur', function(){
validate_field(this);
});
jQuery(this).bind('focus keypress', function(){
jQuery(this).next('.' + options['error_message_class']).fadeOut("fast", function(){
jQuery(this).remove();
});
jQuery(this).parents("." + options['error_container_class']).removeClass('error');
});
}
}
jQuery(this).parents("form").submit(function(){
if (validate_field('#' + SelfID)) {
return true;
}
else
return false;
});
function validate_field(id){
var self = jQuery(id).attr("id");
var expression = 'function Validate(){' + options['expression'].replace(/VAL/g, 'jQuery(\'#' + self + '\').val()') + '} Validate()';
var validation_state = eval(expression);
if (!validation_state) {
if (jQuery(id).next('.' + options['error_message_class']).length == 0) {
jQuery(id).before('<span class="' + options['error_message_class'] + '">' + options['message'] + '</span>');
jQuery(id).parents("div ." + options['error_container_class']).addClass("error")
}
if (ValidationErrors[FormID].join("|").search(id) == -1)
ValidationErrors[FormID].push(id);
return false;
}
else {
for (var i = 0; i < ValidationErrors[FormID].length; i++) {
if (ValidationErrors[FormID][i] == id)
ValidationErrors[FormID].splice(i, 1);
}
return true;
}
}
};
jQuery.fn.validated = function(callback){
jQuery(this).each(function(){
if (this.tagName == "form") {
jQuery(this).submit(function(){
if (ValidationErrors[jQuery(this).attr("id")].length == 0)
callback();
return false;
});
}
});
};
})(jQuery);
/*
* jquery.validate.functions.js - provides helper methods for date and radio button validation to script above
*
* @author GeekTantra
* @date 24 September 2009
*/
/*
* This functions checks where an entered date is valid or not.
* It also works for leap year feb 29ths.
* @year: The Year entered in a date
* @month: The Month entered in a date
* @day: The Day entered in a date
*/
function isValidDate(year, month, day){
var date = new Date(year, (month - 1), day);
var DateYear = date.getFullYear();
var DateMonth = date.getMonth();
var DateDay = date.getDate();
if (DateYear == year && DateMonth == (month - 1) && DateDay == day)
return true;
else
return false;
}
/*
* This function checks if there is at-least one element checked in a group of check-boxes or radio buttons.
* @id: The ID of the check-box or radio-button group
*/
function isChecked(id){
var ReturnVal = false;
$("#" + id).find('input[type="radio"]').each(function(){
if ($(this).is(":checked"))
ReturnVal = true;
});
$("#" + id).find('input[type="checkbox"]').each(function(){
if ($(this).is(":checked"))
ReturnVal = true;
});
return ReturnVal;
}
The function was designed to place a Bootstrap2 formatted error message after (but inline with) the input/selection field container being validated when the BS2 "help-inline" class is specified, or under the field if the BS2 "help-block" class is used. And it works fairly well for basic horizontal forms with spaced out fields and control/submit buttons in their own area.
However, I wanted to use the function with a single field, one-line with submit button directly adjacent to input box, type form (see this site). I would like to have the error message placed before the input field, instead of after it, so as not to break up the unified "one box" input/submit design.
One problem though, I know practically no javascript - all my js knowledge is from reading stuff here and skimming a few intro tutorials over the last few months as time permitted (meaning I could usually only spend an hour or two on it every one or two weeks
Right now the only change I made with the above code was replacing all ".after" calls with ".before", which, to my surprise, actually worked out ... kind of
So ....
oh, before I forget ... how do I display variable values using the js console? print($var)?






Cartoon Clouds
Mountains
Sunrise
Clouds
Green Clouds
None



















Help