Automatically add jQuery validator to all forms.

This commit is contained in:
Josh Sherman 2010-11-15 00:32:33 -05:00
parent ba13bb5696
commit e4afe5209f
3 changed files with 20 additions and 7 deletions

6
js/combined.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1,7 +1,13 @@
$(document).ready(function()
{
// Apply the validator if available
if (jQuery().validate)
{
$('form').validate();
}
// Catches forms being submitted
$('form.ajax input[type=submit],form.ajax button.submit').click(function()
$('form.ajax input[type=submit],form.ajax button.submit').live('click', function()
{
// Grabs the form
var form = $(this).parents('form').get();
@ -12,6 +18,9 @@ $(document).ready(function()
// Sets the buttons, inputs and textareas to READONLY
$('button, input, textarea', form).attr('readonly', 'readonly');
// Forces the cursor to be waiting
document.body.style.cursor = 'wait';
// Copies any CKEditor data to the same named form element (hack)
if (typeof(CKEDITOR) != 'undefined')
{
@ -77,7 +86,11 @@ $(document).ready(function()
injectMessage(form, data, 'error');
}
// Removes READONLY status
$('button, input, textarea', form).attr('readonly', '');
// Returns the cursor to normal... but is anyone really normal?
document.body.style.cursor = 'default';
},
'error': function(XMLHttpRequest, textStatus, errorThrown)

6
js/core.min.js vendored
View file

@ -1,5 +1,5 @@
$(document).ready(function(){$("form.ajax input[type=submit],form.ajax button.submit").click(function(){var a=$(this).parents("form").get();if(typeof a.valid=="undefined"||a.valid()==true){$("button, input, textarea",a).attr("readonly","readonly");if(typeof CKEDITOR!="undefined")if(typeof CKEDITOR.instances!="undefined")for(var d in CKEDITOR.instances){var c=CKEDITOR.instances[d].getData();c!=""&&$("#"+d).val(c)}d=$(a).attr("method")==""?"GET":$(a).attr("method");c=$(a).attr("action");c==""?injectMessage(a,
"Form element lacks action attribute","error"):$.ajax({type:d,url:c,data:$(a).serialize(),dataType:"json",success:function(b){if(b.status!="success"&&typeof b.message!="undefined"){injectMessage(a,b.message,"error");$("button",a).show()}else if(b.status=="success"){$("input[type=text]",a).val("");$("select",a).val("");$("textarea",a).val("");typeof b.message!="undefined"&&injectMessage(a,b.message,"message");if(typeof b.url!="undefined")parent.location.href=b.url;typeof b.callback!="undefined"&&window[b.callback](b)}else injectMessage(a,
b,"error");$("button, input, textarea",a).attr("readonly","")},error:function(){}})}});$("form.ajax").submit(function(){return false});$("table tr:even td").addClass("even");$("table tr:odd td").addClass("odd")});
$(document).ready(function(){jQuery().validate&&$("form").validate();$("form.ajax input[type=submit],form.ajax button.submit").live("click",function(){var a=$(this).parents("form").get();if(typeof a.valid=="undefined"||a.valid()==true){$("button, input, textarea",a).attr("readonly","readonly");document.body.style.cursor="wait";if(typeof CKEDITOR!="undefined")if(typeof CKEDITOR.instances!="undefined")for(var d in CKEDITOR.instances){var c=CKEDITOR.instances[d].getData();c!=""&&$("#"+d).val(c)}d=$(a).attr("method")==
""?"GET":$(a).attr("method");c=$(a).attr("action");c==""?injectMessage(a,"Form element lacks action attribute","error"):$.ajax({type:d,url:c,data:$(a).serialize(),dataType:"json",success:function(b){if(b.status!="success"&&typeof b.message!="undefined"){injectMessage(a,b.message,"error");$("button",a).show()}else if(b.status=="success"){$("input[type=text]",a).val("");$("select",a).val("");$("textarea",a).val("");typeof b.message!="undefined"&&injectMessage(a,b.message,"message");if(typeof b.url!=
"undefined")parent.location.href=b.url;typeof b.callback!="undefined"&&window[b.callback](b)}else injectMessage(a,b,"error");$("button, input, textarea",a).attr("readonly","");document.body.style.cursor="default"},error:function(){}})}});$("form.ajax").submit(function(){return false});$("table tr:even td").addClass("even");$("table tr:odd td").addClass("odd")});
function injectMessage(a,d,c){if(typeof c=="undefined")c="error";switch(c){case "error":var b="#800";break;case "message":b="#080";break;default:b="#000"}c="ajax-form-"+c;b="display:none;color:"+b;$("."+c,a).remove();$(a).prepend('<div class="'+c+'" style="'+b+'">'+d+"</div>");$("."+c,a).fadeIn()}function autoTab(a){$(a).val().length>=$(a).attr("maxlength")&&$(a).next().focus()}function disableEnterKey(a){return(window.event?window.event.keyCode:a.which)!=13}
function truncate(a,d,c){if(a.length>d)a=c==true?'<span title="'+a+'" style="cursor:help">'+a.substring(0,d)+"...</span>":a.substring(0,d)+"...";return a};