diff --git a/jquery.alphanum.js b/jquery.alphanum.js index 1db60ea..4bb452f 100644 --- a/jquery.alphanum.js +++ b/jquery.alphanum.js @@ -8,7 +8,7 @@ // API /////////////////////////////////////////////////////////////////// $.fn.alphanum = function(settings) { - + var combinedSettings = getCombinedSettingsAlphaNum(settings); var $collection = this; @@ -17,9 +17,9 @@ return this; }; - + $.fn.alpha = function(settings) { - + var defaultAlphaSettings = getCombinedSettingsAlphaNum("alpha"); var combinedSettings = getCombinedSettingsAlphaNum(settings, defaultAlphaSettings); @@ -29,9 +29,9 @@ return this; }; - + $.fn.numeric = function(settings) { - + var combinedSettings = getCombinedSettingsNum(settings); var $collection = this; @@ -43,12 +43,12 @@ return this; }; - + // End of API ///////////////////////////////////////////////////////////// - - + + // Start Settings //////////////////////////////////////////////////////// - + var DEFAULT_SETTINGS_ALPHANUM = { allow : '', // Allow extra characters disallow : '', // Disallow extra characters @@ -63,7 +63,7 @@ forceLower : false, // Convert upper case characters to lower case maxLength : NaN // eg Max Length } - + var DEFAULT_SETTINGS_NUM = { allowPlus : false, // Allow the + sign allowMinus : true, // Allow the - sign @@ -76,7 +76,7 @@ max : NaN, // The max numeric value allowed min : NaN // The min numeric value allowed } - + // Some pre-defined groups of settings for convenience var CONVENIENCE_SETTINGS_ALPHANUM = { "alpha" : { @@ -111,27 +111,27 @@ allowDecSep : false } }; - - + + var BLACKLIST = getBlacklistAscii() + getBlacklistNonAscii(); var THOU_SEP = ","; var DEC_SEP = "."; var DIGITS = getDigitsMap(); var LATIN_CHARS = getLatinCharsSet(); - + // Return the blacklisted special chars that are encodable using 7-bit ascii function getBlacklistAscii(){ var blacklist = '!@#$%^&*()+=[]\\\';,/{}|":<>?~`.-_'; blacklist += " "; // 'Space' is on the blacklist but can be enabled using the 'allowSpace' config entry return blacklist; } - + // Return the blacklisted special chars that are NOT encodable using 7-bit ascii // We want this .js file to be encoded using 7-bit ascii so it can reach the widest possible audience // Higher order chars must be escaped eg "\xAC" // Not too worried about comments containing higher order characters for now (let's wait and see if it becomes a problem) function getBlacklistNonAscii(){ - var blacklist = + var blacklist = "\xAC" // ¬ + "\u20AC" // € + "\xA3" // £ @@ -139,10 +139,10 @@ ; return blacklist; } - + // End Settings //////////////////////////////////////////////////////// - - + + // Implementation details go here //////////////////////////////////////////////////////// function setupEventHandlers($textboxes, trimFunction, settings) { @@ -165,7 +165,7 @@ }); $textbox.bind("keypress", function(e){ - + // Determine which key is pressed. // If it's a control key, then allow the event's default action to occur eg backspace, tab var charCode = !e.charCode ? e.which : e.charCode; @@ -182,7 +182,7 @@ var end = selectionObject.end; var textBeforeKeypress = $textbox.val(); - + // The new char may be inserted: // 1) At the start // 2) In the middle @@ -207,7 +207,10 @@ // Ensure the text is a valid number when focus leaves the textbox // This catches the case where a user enters '-' or '.' without entering any digits function numericField_Blur(inputBox, settings) { - var fieldValueNumeric = parseFloat($(inputBox).val()); + + // if input field contains thousand seprator then ignore it + var fieldValueNumeric = parseFloat($(inputBox).val().replace(THOU_SEP, '')); + var $inputBox = $(inputBox); if(isNaN(fieldValueNumeric)) { @@ -237,35 +240,35 @@ return true; } - + // One way to prevent a character being entered is to cancel the keypress event. // However, this gets messy when you have to deal with things like copy paste which isn't a keypress. // Which event gets fired first, keypress or keyup? What about IE6 etc etc? // Instead, it's easier to allow the 'bad' character to be entered and then to delete it immediately after. - + function trimTextbox($textBox, trimFunction, settings, pastedText){ - + var inputString = $textBox.val(); if(inputString == "" && pastedText.length > 0) inputString = pastedText; - + var outputString = trimFunction(inputString, settings); - + if(inputString == outputString) return; - + var caretPos = $textBox.alphanum_caret(); - + $textBox.val(outputString); - + //Reset the caret position if(inputString.length ==(outputString.length + 1)) $textBox.alphanum_caret(caretPos - 1); else $textBox.alphanum_caret(caretPos); } - + function getCombinedSettingsAlphaNum(settings, defaultSettings){ if(typeof defaultSettings == "undefined") defaultSettings = DEFAULT_SETTINGS_ALPHANUM; @@ -276,15 +279,15 @@ userSettings = {}; else userSettings = settings; - + $.extend(combinedSettings, defaultSettings, userSettings); - + if(typeof combinedSettings.blacklist == 'undefined') combinedSettings.blacklistSet = getBlacklistSet(combinedSettings.allow, combinedSettings.disallow); - + return combinedSettings; } - + function getCombinedSettingsNum(settings){ var userSettings, combinedSettings = {}; if(typeof settings === "string") @@ -293,13 +296,13 @@ userSettings = {}; else userSettings = settings; - + $.extend(combinedSettings, DEFAULT_SETTINGS_NUM, userSettings); - + return combinedSettings; } - - + + // This is the heart of the algorithm function alphanum_allowChar(validatedStringFragment, Char, settings){ @@ -308,38 +311,38 @@ if(settings.allow.indexOf(Char) >=0 ) return true; - + if(settings.allowSpace && (Char == " ")) return true; - + if(settings.blacklistSet.contains(Char)) return false; - + if(!settings.allowNumeric && DIGITS[Char]) return false; - + if(!settings.allowUpper && isUpper(Char)) return false; - + if(!settings.allowLower && isLower(Char)) return false; - + if(!settings.allowCaseless && isCaseless(Char)) return false; - + if(!settings.allowLatin && LATIN_CHARS.contains(Char)) return false; - + if(!settings.allowOtherCharSets){ if(DIGITS[Char] || LATIN_CHARS.contains(Char)) return true; else return false; } - + return true; } - + function numeric_allowChar(validatedStringFragment, Char, settings){ if(DIGITS[Char]) { @@ -378,7 +381,7 @@ if(settings.allowDecSep) return true; } - + return false; } @@ -470,20 +473,20 @@ return false; } - + /******************************** * Trims a string according to the settings provided ********************************/ function trimAlphaNum(inputString, settings){ - + if(typeof inputString != "string") return inputString; - + var inChars = inputString.split(""); var outChars = []; var i = 0; var Char; - + for(i=0; i