From 2721d9edfbe22623307feb6915bf8738ec04b612 Mon Sep 17 00:00:00 2001 From: Gareth Elms Date: Sun, 15 Jan 2012 19:32:57 +0000 Subject: [PATCH] Error messages can be split across multiple lines using options.newLineAtCharacterCount --- jquery.formError.js | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/jquery.formError.js b/jquery.formError.js index 24e9fcc..e61a970 100644 --- a/jquery.formError.js +++ b/jquery.formError.js @@ -1,4 +1,4 @@ -// jQuery.formError v0.1 +// jQuery.formError v0.2 // // Copyright (C)2012 Gareth Elms // Distributed under MIT License @@ -40,7 +40,7 @@ { if( this.hasClass('invalid')) //Used to be invalid { - var img = $( ""); + var img = $( ""); this.after( img.fadeIn()); } else @@ -70,8 +70,10 @@ remove.call( this, {successImage: {disabled:true}}); // Just remove the previous error message if it exists, we are replacing it now removeSuccessImage.call( this); // Also remove the success image if present + options.message = options.message.injectNewLines( options.newLineAtCharacterCount); + var errorDiv = - $("
" + + $("
" + "" + "
" + options.message + @@ -110,10 +112,42 @@ $.fn.formError.defaultOptions = { + newLineAtCharacterCount: 30, successImage: { enabled:true, src: "success.gif" } }; -})( jQuery ); \ No newline at end of file +})( jQuery ); + + +String.prototype.injectNewLines = + function( maxLineLength) + { + if( typeof( maxLineLength) == "number" && maxLineLength > 0) + { + var tempMessage = ""; + var lineLength = 0; + var words = this.split( /\s+/); + for( var word in words) + { + tempMessage += words[word]; + lineLength += words[word].length; + if( lineLength > maxLineLength) + { + tempMessage += "
"; + lineLength = 0; + } + else + { + tempMessage += " "; + lineLength ++; + } + } + + return tempMessage; + } + + return this; + }; \ No newline at end of file