1/** 2* autoNumeric.js 3* @author: Bob Knothe 4* @author: Sokolov Yura 5* @version: 1.9.24 - 2014-06-29 GMT 10:30 AM 6* 7* Created by Robert J. Knothe on 2010-10-25. Please report any bugs to https://github.com/BobKnothe/autoNumeric 8* Created by Sokolov Yura on 2010-11-07 9* 10* Copyright (c) 2011 Robert J. Knothe http://www.decorplanit.com/plugin/ 11* 12* The MIT License (http://www.opensource.org/licenses/mit-license.php) 13* 14* Permission is hereby granted, free of charge, to any person 15* obtaining a copy of this software and associated documentation 16* files (the "Software"), to deal in the Software without 17* restriction, including without limitation the rights to use, 18* copy, modify, merge, publish, distribute, sublicense, and/or sell 19* copies of the Software, and to permit persons to whom the 20* Software is furnished to do so, subject to the following 21* conditions: 22* 23* The above copyright notice and this permission notice shall be 24* included in all copies or substantial portions of the Software. 25* 26* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 28* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 30* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 31* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 32* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 33* OTHER DEALINGS IN THE SOFTWARE. 34*/ 35(function ($) { 36 "use strict"; 37 /*jslint browser: true*/ 38 /*global jQuery: false*/ 39 /* Cross browser routine for getting selected range/cursor position 40 */ 41 function getElementSelection(that) { 42 var position = {}; 43 if (that.selectionStart === undefined) { 44 that.focus(); 45 var select = document.selection.createRange(); 46 position.length = select.text.length; 47 select.moveStart('character', -that.value.length); 48 position.end = select.text.length; 49 position.start = position.end - position.length; 50 } else { 51 position.start = that.selectionStart; 52 position.end = that.selectionEnd; 53 position.length = position.end - position.start; 54 } 55 return position; 56 } 57 /** 58 * Cross browser routine for setting selected range/cursor position 59 */ 60 function setElementSelection(that, start, end) { 61 if (that.selectionStart === undefined) { 62 that.focus(); 63 var r = that.createTextRange(); 64 r.collapse(true); 65 r.moveEnd('character', end); 66 r.moveStart('character', start); 67 r.select(); 68 } else { 69 that.selectionStart = start; 70 that.selectionEnd = end; 71 } 72 } 73 /** 74 * run callbacks in parameters if any 75 * any parameter could be a callback: 76 * - a function, which invoked with jQuery element, parameters and this parameter name and returns parameter value 77 * - a name of function, attached to $(selector).autoNumeric.functionName(){} - which was called previously 78 */ 79 function runCallbacks($this, settings) { 80 /** 81 * loops through the settings object (option array) to find the following 82 * k = option name example k=aNum 83 * val = option value example val=0123456789 84 */ 85 $.each(settings, function (k, val) { 86 if (typeof val === 'function') { 87 settings[k] = val($this, settings, k); 88 } else if (typeof $this.autoNumeric[val] === 'function') { 89 /** 90 * calls the attached function from the html5 data example: data-a-sign="functionName" 91 */ 92 settings[k] = $this.autoNumeric[val]($this, settings, k); 93 } 94 }); 95 } 96 function convertKeyToNumber(settings, key) { 97 if (typeof (settings[key]) === 'string') { 98 settings[key] *= 1; 99 } 100 } 101 /** 102 * Preparing user defined options for further usage 103 * merge them with defaults appropriately 104 */ 105 function autoCode($this, settings) { 106 runCallbacks($this, settings); 107 settings.oEvent = null; 108 settings.tagList = ['b', 'caption', 'cite', 'code', 'dd', 'del', 'div', 'dfn', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ins', 'kdb', 'label', 'li', 'output', 'p', 'q', 's', 'sample', 'span', 'strong', 'td', 'th', 'u', 'var']; 109 var vmax = settings.vMax.toString().split('.'), 110 vmin = (!settings.vMin && settings.vMin !== 0) ? [] : settings.vMin.toString().split('.'); 111 convertKeyToNumber(settings, 'vMax'); 112 convertKeyToNumber(settings, 'vMin'); 113 convertKeyToNumber(settings, 'mDec'); /** set mDec if not defined by user */ 114 settings.mDec = (settings.mRound === 'CHF') ? '2' : settings.mDec; 115 settings.allowLeading = true; 116 settings.aNeg = settings.vMin < 0 ? '-' : ''; 117 vmax[0] = vmax[0].replace('-', ''); 118 vmin[0] = vmin[0].replace('-', ''); 119 settings.mInt = Math.max(vmax[0].length, vmin[0].length, 1); 120 if (settings.mDec === null) { 121 var vmaxLength = 0, 122 vminLength = 0; 123 if (vmax[1]) { 124 vmaxLength = vmax[1].length; 125 } 126 if (vmin[1]) { 127 vminLength = vmin[1].length; 128 } 129 settings.mDec = Math.max(vmaxLength, vminLength); 130 } /** set alternative decimal separator key */ 131 if (settings.altDec === null && settings.mDec > 0) { 132 if (settings.aDec === '.' && settings.aSep !== ',') { 133 settings.altDec = ','; 134 } else if (settings.aDec === ',' && settings.aSep !== '.') { 135 settings.altDec = '.'; 136 } 137 } 138 /** cache regexps for autoStrip */ 139 var aNegReg = settings.aNeg ? '([-\\' + settings.aNeg + ']?)' : '(-?)'; 140 settings.aNegRegAutoStrip = aNegReg; 141 settings.skipFirstAutoStrip = new RegExp(aNegReg + '[^-' + (settings.aNeg ? '\\' + settings.aNeg : '') + '\\' + settings.aDec + '\\d]' + '.*?(\\d|\\' + settings.aDec + '\\d)'); 142 settings.skipLastAutoStrip = new RegExp('(\\d\\' + settings.aDec + '?)[^\\' + settings.aDec + '\\d]\\D*$'); 143 var allowed = '-' + settings.aNum + '\\' + settings.aDec; 144 settings.allowedAutoStrip = new RegExp('[^' + allowed + ']', 'gi'); 145 settings.numRegAutoStrip = new RegExp(aNegReg + '(?:\\' + settings.aDec + '?(\\d+\\' + settings.aDec + '\\d+)|(\\d*(?:\\' + settings.aDec + '\\d*)?))'); 146 return settings; 147 } 148 /** 149 * strip all unwanted characters and leave only a number alert 150 */ 151 function autoStrip(s, settings, strip_zero) { 152 if (settings.aSign) { /** remove currency sign */ 153 while (s.indexOf(settings.aSign) > -1) { 154 s = s.replace(settings.aSign, ''); 155 } 156 } 157 s = s.replace(settings.skipFirstAutoStrip, '$1$2'); /** first replace anything before digits */ 158 s = s.replace(settings.skipLastAutoStrip, '$1'); /** then replace anything after digits */ 159 s = s.replace(settings.allowedAutoStrip, ''); /** then remove any uninterested characters */ 160 if (settings.altDec) { 161 s = s.replace(settings.altDec, settings.aDec); 162 } /** get only number string */ 163 var m = s.match(settings.numRegAutoStrip); 164 s = m ? [m[1], m[2], m[3]].join('') : ''; 165 if ((settings.lZero === 'allow' || settings.lZero === 'keep') && str
165ip_zero !== 'strip') { 166 var parts = [], 167 nSign = ''; 168 parts = s.split(settings.aDec); 169 if (parts[0].indexOf('-') !== -1) { 170 nSign = '-'; 171 parts[0] = parts[0].replace('-', ''); 172 } 173 if (parts[0].length > settings.mInt && parts[0].charAt(0) === '0') { /** strip leading zero if need */ 174 parts[0] = parts[0].slice(1); 175 } 176 s = nSign + parts.join(settings.aDec); 177 } 178 if ((strip_zero && settings.lZero === 'deny') || (strip_zero && settings.lZero === 'allow' && settings.allowLeading === false)) { 179 var strip_reg = '^' + settings.aNegRegAutoStrip + '0*(\\d' + (strip_zero === 'leading' ? ')' : '|$)'); 180 strip_reg = new RegExp(strip_reg); 181 s = s.replace(strip_reg, '$1$2'); 182 } 183 return s; 184 } 185 /** 186 * places or removes brackets on negative values 187 */ 188 function negativeBracket(s, nBracket, oEvent) { /** oEvent = settings.oEvent */ 189 nBracket = nBracket.split(','); 190 if (oEvent === 'set' || oEvent === 'focusout') { 191 s = s.replace('-', ''); 192 s = nBracket[0] + s + nBracket[1]; 193 } else if ((oEvent === 'get' || oEvent === 'focusin' || oEvent === 'pageLoad') && s.charAt(0) === nBracket[0]) { 194 s = s.replace(nBracket[0], '-'); 195 s = s.replace(nBracket[1], ''); 196 } 197 return s; 198 } 199 /** 200 * truncate decimal part of a number 201 */ 202 function truncateDecimal(s, aDec, mDec) { 203 if (aDec && mDec) { 204 var parts = s.split(aDec); 205 /** truncate decimal part to satisfying length 206 * cause we would round it anyway */ 207 if (parts[1] && parts[1].length > mDec) { 208 if (mDec > 0) { 209 parts[1] = parts[1].substring(0, mDec); 210 s = parts.join(aDec); 211 } else { 212 s = parts[0]; 213 } 214 } 215 } 216 return s; 217 } 218 /** 219 * prepare number string to be converted to real number 220 */ 221 function fixNumber(s, aDec, aNeg) { 222 if (aDec && aDec !== '.') { 223 s = s.replace(aDec, '.'); 224 } 225 if (aNeg && aNeg !== '-') { 226 s = s.replace(aNeg, '-'); 227 } 228 if (!s.match(/\d/)) { 229 s += '0'; 230 } 231 return s; 232 } 233 /** 234 * function to handle numbers less than 0 that are stored in Exponential notation ex: .0000001 stored as 1e-7 235 */ 236 function checkValue(value, settings) { 237 if (value) { 238 var checkSmall = +value; 239 if (checkSmall < 0.000001 && checkSmall > -1) { 240 value = +value; 241 if (value < 0.000001 && value > 0) { 242 value = (value + 10).toString(); 243 value = value.substring(1); 244 } 245 if (value < 0 && value > -1) { 246 value = (value - 10).toString(); 247 value = '-' + value.substring(2); 248 } 249 value = value.toString(); 250 } else { 251 var parts = value.split('.'); 252 if (parts[1] !== undefined) { 253 if (+parts[1] === 0) { 254 value = parts[0]; 255 } else { 256 parts[1] = parts[1].replace(/0*$/, ''); 257 value = parts.join('.'); 258 } 259 } 260 } 261 } 262 return (settings.lZero === 'keep') ? value : value.replace(/^0*(\d)/, '$1'); 263 } 264 /** 265 * prepare real number to be converted to our format 266 */ 267 function presentNumber(s, aDec, aNeg) { 268 if (aNeg && aNeg !== '-') { 269 s = s.replace('-', aNeg); 270 } 271 if (aDec && aDec !== '.') { 272 s = s.replace('.', aDec); 273 } 274 return s; 275 } 276 /** 277 * checking that number satisfy format conditions 278 * and lays between settings.vMin and settings.vMax 279 * and the string length does not exceed the digits in settings.vMin and settings.vMax 280 */ 281 function autoCheck(s, settings) { 282 s = autoStrip(s, settings); 283 s = truncateDecimal(s, settings.aDec, settings.mDec); 284 s = fixNumber(s, settings.aDec, settings.aNeg); 285 var value = +s; 286 if (settings.oEvent === 'set' && (value < settings.vMin || value > settings.vMax)) { 287 $.error("The value (" + value + ") from the 'set' method falls outside of the vMin / vMax range"); 288 } 289 return value >= settings.vMin && value <= settings.vMax; 290 } 291 /** 292 * private function to check for empty value 293 */ 294 function checkEmpty(iv, settings, signOnEmpty) { 295 if (iv === '' || iv === settings.aNeg) { 296 if (settings.wEmpty === 'zero') { 297 return iv + '0'; 298 } 299 if (settings.wEmpty === 'sign' || signOnEmpty) { 300 return iv + settings.aSign; 301 } 302 return iv; 303 } 304 return null; 305 } 306 /** 307 * private function that formats our number 308 */ 309 function autoGroup(iv, settings) { 310 iv = autoStrip(iv, settings); 311 var testNeg = iv.replace(',', '.'), 312 empty = checkEmpty(iv, settings, true); 313 if (empty !== null) { 314 return empty; 315 } 316 var digitalGroup = ''; 317 if (settings.dGroup === 2) { 318 digitalGroup = /(\d)((\d)(\d{2}?)+)$/; 319 } else if (settings.dGroup === 4) { 320 digitalGroup = /(\d)((\d{4}?)+)$/; 321 } else { 322 digitalGroup = /(\d)((\d{3}?)+)$/; 323 } /** splits the string at the decimal string */ 324 var ivSplit = iv.split(settings.aDec); 325 if (settings.altDec && ivSplit.length === 1) { 326 ivSplit = iv.split(settings.altDec); 327 } /** assigns the whole number to the a varibale (s) */ 328 var s = ivSplit[0]; 329 if (settings.aSep) { 330 while (digitalGroup.test(s)) { /** re-inserts the thousand sepparator via a regualer expression */ 331 s = s.replace(digitalGroup, '$1' + settings.aSep + '$2'); 332 } 333 } 334 if (settings.mDec !== 0 && ivSplit.length > 1) { 335 if (ivSplit[1].length > settings.mDec) { 336 ivSplit[1] = ivSplit[1].substring(0, settings.mDec); 337 } /** joins the whole number with the deciaml value */ 338 iv = s + settings.aDec + ivSplit[1]; 339 } else { /** if whole numbers only */ 340 iv = s; 341 } 342 if (settings.aSign) { 343 var has_aNeg = iv.indexOf(settings.aNeg) !== -1; 344 iv = iv.replace(settings.aNeg, ''); 345 iv = settings.pSign === 'p' ? settings.aSign + iv : iv + settings.aSign; 346 if (has_aNeg) { 347 iv = settings.aNeg + iv; 348 } 349 } 350 if (settings.oEvent === 'set' && testNeg < 0 && settings.nBracket !== null) { /** removes the negative sign and places brackets */ 351 iv = negativeBracket(iv, settings.nBracket, settings.oEvent); 352 } 353 return iv; 354 } 355 /** 356 * round number after setting by pasting or $().autoNumericSet()
357 * private function for round the number 358 * please note this handled as text - JavaScript math function can return inaccurate values 359 * also this offers multiple rounding methods that are not easily accomplished in JavaScript 360 */ 361 function autoRound(iv, settings) { /** value to string */ 362 iv = (iv === '') ? '0' : iv.toString(); 363 convertKeyToNumber(settings, 'mDec'); /** set mDec to number needed when mDec set by 'update method */ 364 if (settings.mRound === 'CHF') { 365 iv = (Math.round(iv * 20) / 20).toString(); 366 } 367 var ivRounded = '', 368 i = 0, 369 nSign = '', 370 rDec = (typeof (settings.aPad) === 'boolean' || settings.aPad === null) ? (settings.aPad ? settings.mDec : 0) : +settings.aPad; 371 var truncateZeros = function (ivRounded) { /** truncate not needed zeros */ 372 var regex = (rDec === 0) ? (/(\.(?:\d*[1-9])?)0*$/) : rDec === 1 ? (/(\.\d(?:\d*[1-9])?)0*$/) : new RegExp('(\\.\\d{' + rDec + '}(?:\\d*[1-9])?)0*$'); 373 ivRounded = ivRounded.replace(regex, '$1'); /** If there are no decimal places, we don't need a decimal point at the end */ 374 if (rDec === 0) { 375 ivRounded = ivRounded.replace(/\.$/, ''); 376 } 377 return ivRounded; 378 }; 379 if (iv.charAt(0) === '-') { /** Checks if the iv (input Value)is a negative value */ 380 nSign = '-'; 381 iv = iv.replace('-', ''); /** removes the negative sign will be added back later if required */ 382 } 383 if (!iv.match(/^\d/)) { /** append a zero if first character is not a digit (then it is likely to be a dot)*/ 384 iv = '0' + iv; 385 } 386 if (nSign === '-' && +iv === 0) { /** determines if the value is zero - if zero no negative sign */ 387 nSign = ''; 388 } 389 if ((+iv > 0 && settings.lZero !== 'keep') || (iv.length > 0 && settings.lZero === 'allow')) { /** trims leading zero's if needed */ 390 iv = iv.replace(/^0*(\d)/, '$1'); 391 } 392 var dPos = iv.lastIndexOf('.'), /** virtual decimal position */ 393 vdPos = (dPos === -1) ? iv.length - 1 : dPos, /** checks decimal places to determine if rounding is required */ 394 cDec = (iv.length - 1) - vdPos; /** check if no rounding is required */ 395 if (cDec <= settings.mDec) { 396 ivRounded = iv; /** check if we need to pad with zeros */ 397 if (cDec < rDec) { 398 if (dPos === -1) { 399 ivRounded += '.'; 400 } 401 var zeros = '000000'; 402 while (cDec < rDec) { 403 zeros = zeros.substring(0, rDec - cDec); 404 ivRounded += zeros; 405 cDec += zeros.length; 406 } 407 } else if (cDec > rDec) { 408 ivRounded = truncateZeros(ivRounded); 409 } else if (cDec === 0 && rDec === 0) { 410 ivRounded = ivRounded.replace(/\.$/, ''); 411 } 412 if (settings.mRound !== 'CHF') { 413 return (+ivRounded === 0) ? ivRounded : nSign + ivRounded; 414 } 415 if (settings.mRound === 'CHF') { 416 dPos = ivRounded.lastIndexOf('.'); 417 iv = ivRounded; 418 } 419 420 } /** rounded length of the string after rounding */ 421 var rLength = dPos + settings.mDec, 422 tRound = +iv.charAt(rLength + 1), 423 ivArray = iv.substring(0, rLength + 1).split(''), 424 odd = (iv.charAt(rLength) === '.') ? (iv.charAt(rLength - 1) % 2)
424: (iv.charAt(rLength) % 2), 425 onePass = true; 426 odd = (odd === 0 && (iv.substring(rLength + 2, iv.length) > 0)) ? 1 : 0; 427 if ((tRound > 4 && settings.mRound === 'S') || /** Round half up symmetric */ 428 (tRound > 4 && settings.mRound === 'A' && nSign === '') || /** Round half up asymmetric positive values */ 429 (tRound > 5 && settings.mRound === 'A' && nSign === '-') || /** Round half up asymmetric negative values */ 430 (tRound > 5 && settings.mRound === 's') || /** Round half down symmetric */ 431 (tRound > 5 && settings.mRound === 'a' && nSign === '') || /** Round half down asymmetric positive values */ 432 (tRound > 4 && settings.mRound === 'a' && nSign === '-') || /** Round half down asymmetric negative values */ 433 (tRound > 5 && settings.mRound === 'B') || /** Round half even "Banker's Rounding" */ 434 (tRound === 5 && settings.mRound === 'B' && odd === 1) || /** Round half even "Banker's Rounding" */ 435 (tRound > 0 && settings.mRound === 'C' && nSign === '') || /** Round to ceiling toward positive infinite */ 436 (tRound > 0 && settings.mRound === 'F' && nSign === '-') || /** Round to floor toward negative infinite */ 437 (tRound > 0 && settings.mRound === 'U') || 438 (settings.mRound === 'CHF')) { /** round up away from zero */ 439 for (i = (ivArray.length - 1); i >= 0; i -= 1) { /** Round up the last digit if required, and continue until no more 9's are found */ 440 if (ivArray[i] !== '.') { 441 if (settings.mRound === 'CHF' && ivArray[i] <= 2 && onePass) { 442 ivArray[i] = 0; 443 onePass = false; 444 break; 445 } 446 if (settings.mRound === 'CHF' && ivArray[i] <= 7 && onePass) { 447 ivArray[i] = 5; 448 onePass = false; 449 break; 450 } 451 if (settings.mRound === 'CHF' && onePass) { 452 ivArray[i] = 10; 453 onePass = false; 454 } else { 455 ivArray[i] = +ivArray[i] + 1; 456 } 457 if (ivArray[i] < 10) { 458 break; 459 } 460 if (i > 0) { 461 ivArray[i] = '0'; 462 } 463 } 464 } 465 } 466 ivArray = ivArray.slice(0, rLength + 1); /** Reconstruct the string, converting any 10's to 0's */ 467 ivRounded = truncateZeros(ivArray.join('')); /** return rounded value */ 468 return (+ivRounded === 0) ? ivRounded : nSign + ivRounded; 469 } 470 /** 471 * Holder object for field properties 472 */ 473 function AutoNumericHolder(that, settings) { 474 this.settings = settings; 475 this.that = that; 476 this.$that = $(that); 477 this.formatted = false; 478 this.settingsClone = autoCode(this.$that, this.settings); 479 this.value = that.value; 480 } 481 AutoNumericHolder.prototype = { 482 init: function (e) { 483 this.value = this.that.value; 484 this.settingsClone = autoCode(this.$that, this.settings); 485 this.ctrlKey = e.ctrlKey; 486 this.cmdKey = e.metaKey; 487 this.shiftKey = e.shiftKey; 488 this.selection = getElementSelection(this.that); /** keypress event overwrites meaningful value of e.keyCode */ 489 if (e.type === 'keydown' || e.type === 'keyup') { 490 this.kdCode = e.keyCode; 491 } 492 this.which = e.which; 493 this.processed = false; 494 this.formatted = false; 495 }, 496 setSelection: function (start, end, setReal) { 497 start = Math.max(start, 0); 498 end = Math.min(end, this.that.value.length); 499 this.selection = { 500 start: start, 501 end: end, 502 length: end - start 503 }; 504 if (setReal === undefined || setReal) { 505 setElementSelection(this.that, start, end); 506 } 507 }, 508 setPosition: function (pos, setReal) { 509 this.setSelection(pos, pos, setReal); 510 }, 511 getBeforeAfter: function () { 512 var value = this.value, 513 left = value.substring(0, this.selection.start), 514 right = value.substring(this.selection.end, value.length); 515 return [left, right]; 516 }, 517 getBeforeAfterStriped: function () { 518 var parts = this.getBeforeAfter(); 519 parts[0] = autoStrip(parts[0], this.settingsClone); 520 parts[1] = autoStrip(parts[1], this.settingsClone); 521 return parts; 522 }, 523 /** 524 * strip parts from excess characters and leading zeroes 525 */ 526 normalizeParts: function (left, right) { 527 var settingsClone = this.settingsClone; 528 right = autoStrip(right, settingsClone); /** if right is not empty and first character is not aDec, */ 529 /** we could strip all zeros, otherwise only leading */ 530 var strip = right.match(/^\d/) ? true : 'leading'; 531 left = autoStrip(left, settingsClone, strip); /** prevents multiple leading zeros from being entered */ 532 if ((left === '' || left === settingsClone.aNeg) && settingsClone.lZero === 'deny') { 533 if (right > '') { 534 right = right.replace(/^0*(\d)/, '$1'); 535 } 536 } 537 var new_value = left + right; /** insert zero if has leading dot */ 538 if (settingsClone.aDec) { 539 var m = new_value.match(new RegExp('^' + settingsClone.aNegRegAutoStrip + '\\' + settingsClone.aDec)); 540 if (m) { 541 left = left.replace(m[1], m[1] + '0'); 542 new_value = left + right; 543 } 544 } /** insert zero if number is empty and io.wEmpty == 'zero' */ 545 if (settingsClone.wEmpty === 'zero' && (new_value === settingsClone.aNeg || new_value === '')) { 546 left += '0'; 547 } 548 return [left, right]; 549 }, 550 /** 551 * set part of number to value keeping position of cursor 552 */ 553 setValueParts: function (left, right) { 554 var settingsClone = this.settingsClone, 555 parts = this.normalizeParts(left, right), 556 new_value = parts.join(''), 557 position = parts[0].length; 558 if (autoCheck(new_value, settingsClone)) { 559 new_value = truncateDecimal(new_value, settingsClone.aDec, settingsClone.mDec); 560 if (position > new_value.length) { 561 position = new_value.length; 562 } 563 this.value = new_value; 564 this.setPosition(position, false); 565 return true; 566 } 567 return false;
568 }, 569 /** 570 * helper function for expandSelectionOnSign 571 * returns sign position of a formatted value 572 */ 573 signPosition: function () { 574 var settingsClone = this.settingsClone, 575 aSign = settingsClone.aSign, 576 that = this.that; 577 if (aSign) { 578 var aSignLen = aSign.length; 579 if (settingsClone.pSign === 'p') { 580 var hasNeg = settingsClone.aNeg && that.value && that.value.charAt(0) === settingsClone.aNeg; 581 return hasNeg ? [1, aSignLen + 1] : [0, aSignLen]; 582 } 583 var valueLen = that.value.length; 584 return [valueLen - aSignLen, valueLen]; 585 } 586 return [1000, -1]; 587 }, 588 /** 589 * expands selection to cover whole sign 590 * prevents partial deletion/copying/overwriting of a sign 591 */ 592 expandSelectionOnSign: function (setReal) { 593 var sign_position = this.signPosition(), 594 selection = this.selection; 595 if (selection.start < sign_position[1] && selection.end > sign_position[0]) { /** if selection catches something except sign and catches only space from sign */ 596 if ((selection.start < sign_position[0] || selection.end > sign_position[1]) && this.value.substring(Math.max(selection.start, sign_position[0]), Math.min(selection.end, sign_position[1])).match(/^\s*$/)) { /** then select without empty space */ 597 if (selection.start < sign_position[0]) { 598 this.setSelection(selection.start, sign_position[0], setReal); 599 } else { 600 this.setSelection(sign_position[1], selection.end, setReal); 601 } 602 } else { /** else select with whole sign */ 603 this.setSelection(Math.min(selection.start, sign_position[0]), Math.max(selection.end, sign_position[1]), setReal); 604 } 605 } 606 }, 607 /** 608 * try to strip pasted value to digits 609 */ 610 checkPaste: function () { 611 if (this.valuePartsBeforePaste !== undefined) { 612 var parts = this.getBeforeAfter(), 613 oldParts = this.valuePartsBeforePaste; 614 delete this.valuePartsBeforePaste; /** try to strip pasted value first */ 615 parts[0] = parts[0].substr(0, oldParts[0].length) + autoStrip(parts[0].substr(oldParts[0].length), this.settingsClone); 616 if (!this.setValueParts(parts[0], parts[1])) { 617 this.value = oldParts.join(''); 618 this.setPosition(oldParts[0].length, false); 619 } 620 } 621 }, 622 /** 623 * process pasting, cursor moving and skipping of not interesting keys 624 * if returns true, further processing is not performed 625 */ 626 skipAllways: function (e) { 627 var kdCode = this.kdCode, 628 which = this.which, 629 ctrlKey = this.ctrlKey, 630 cmdKey = this.cmdKey, 631 shiftKey = this.shiftKey; /** catch the ctrl up on ctrl-v */ 632 if (((ctrlKey || cmdKey) && e.type === 'keyup' && this.valuePartsBeforePaste !== undefined) || (shiftKey && kdCode === 45)) { 633 this.checkPaste(); 634 return false; 635 } 636 /** codes are taken from http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx 637 * skip Fx keys, windows keys, other special keys 638 */ 639 if ((kdCode >= 112 && kdCode <= 123) || (kdCode >= 91 && kdCode <= 93) || (kdCode >= 9 && kdCode <= 31) || (kdCode < 8 && (which === 0 || which === kdCode)) || kdCode === 144 || kdCode === 145 || kdCode === 45) { 640 return true; 641 } 642 if ((ctrlKey || cmdKey) && kdCode === 65) { /** if select all (a=65)*/ 643 return true; 644 } 645 if ((ctrlKey || cmdKey) && (kdCode === 67 || kdCode === 86 || kdCode === 88)) { /** if copy (c=67) paste (v=86) or cut (x=88) */ 646 if (e.type === 'keydown') { 647 this.expandSelectionOnSign(); 648 } 649 if (kdCode === 86 || kdCode === 45) { /** try to prevent wrong paste */ 650 if (e.type === 'keydown' || e.type === 'keypress') { 651 if (this.valuePartsBeforePaste === undefined) { 652 this.valuePartsBeforePaste = this.getBeforeAfter(); 653 } 654 } else { 655 this.checkPaste(); 656 } 657 } 658 return e.type === 'keydown' || e.type === 'keypress' || kdCode === 67; 659 } 660 if (ctrlKey || cmdKey) { 661 return true; 662 } 663 if (kdCode === 37 || kdCode === 39) { /** jump over thousand separator */ 664 var aSep = this.settingsClone.aSep, 665 start = this.selection.start, 666 value = this.that.value; 667 if (e.type === 'keydown' && aSep && !this.shiftKey) { 668 if (kdCode === 37 && value.charAt(start - 2) === aSep) { 669 this.setPosition(start - 1); 670 } else if (kdCode === 39 && value.charAt(start + 1) === aSep) { 671 this.setPosition(start + 1); 672 } 673 } 674 return true; 675 } 676 if (kdCode >= 34 && kdCode <= 40) { 677 return true; 678 } 679 return false;
680 }, 681 /** 682 * process deletion of characters 683 * returns true if processing performed 684 */ 685 processAllways: function () { 686 var parts; /** process backspace or delete */ 687 if (this.kdCode === 8 || this.kdCode === 46) { 688 if (!this.selection.length) { 689 parts = this.getBeforeAfterStriped(); 690 if (this.kdCode === 8) { 691 parts[0] = parts[0].substring(0, parts[0].length - 1); 692 } else { 693 parts[1] = parts[1].substring(1, parts[1].length); 694 } 695 this.setValueParts(parts[0], parts[1]); 696 } else { 697 this.expandSelectionOnSign(false); 698 parts = this.getBeforeAfterStriped(); 699 this.setValueParts(parts[0], parts[1]); 700 } 701 return true; 702 } 703 return false; 704 }, 705 /** 706 * process insertion of characters 707 * returns true if processing performed 708 */ 709 processKeypress: function () { 710 var settingsClone = this.settingsClone, 711 cCode = String.fromCharCode(this.which), 712 parts = this.getBeforeAfterStriped(), 713 left = parts[0], 714 right = parts[1]; /** start rules when the decimal character key is pressed */ 715 /** always use numeric pad dot to insert decimal separator */ 716 if (cCode === settingsClone.aDec || (settingsClone.altDec && cCode === settingsClone.altDec) || ((cCode === '.' || cCode === ',') && this.kdCode === 110)) { /** do not allow decimal character if no decimal part allowed */ 717 if (!settingsClone.mDec || !settingsClone.aDec) { 718 return true; 719 } /** do not allow decimal character before aNeg character */ 720 if (settingsClone.aNeg && right.indexOf(settingsClone.aNeg) > -1) { 721 return true; 722 } /** do not allow decimal character if other decimal character present */ 723 if (left.indexOf(settingsClone.aDec) > -1) { 724 return true; 725 } 726 if (right.indexOf(settingsClone.aDec) > 0) { 727 return true; 728 } 729 if (right.indexOf(settingsClone.aDec) === 0) { 730 right = right.substr(1); 731 } 732 this.setValueParts(left + settingsClone.aDec, right); 733 return true; 734 } 735 /** 736 * start rule on negative sign & prevent minus if not allowed 737 */ 738 if (cCode === '-' || cCode === '+') { 739 if (!settingsClone.aNeg) { 740 return true; 741 } /** caret is always after minus */ 742 if (left === '' && right.indexOf(settingsClone.aNeg) > -1) { 743 left = settingsClone.aNeg; 744 right = right.substring(1, right.length); 745 } /** change sign of number, remove part if should */ 746 if (left.charAt(0) === settingsClone.aNeg) { 747 left = left.substring(1, left.length); 748 } else { 749 left = (cCode === '-') ? settingsClone.aNeg + left : left; 750 } 751 this.setValueParts(left, right); 752 return true; 753 } /** digits */ 754 if (cCode >= '0' && cCode <= '9') { /** if try to insert digit before minus */ 755 if (settingsClone.aNeg && left === '' && right.indexOf(settingsClone.aNeg) > -1) { 756 left = settingsClone.aNeg; 757 right = right.substring(1, right.length); 758 } 759 if (settingsClone.vMax <= 0 && settingsClone.vMin < settingsClone.vMax && this.value.indexOf(settingsClone.aNeg) === -1 && cCode !== '0') { 760 left = settingsClone.aNeg + left; 761 } 762 this.setValueParts(left + cCode, right); 763 return true; 764 } /** prevent any other character */ 765 return true; 766 }, 767 /** 768 * formatting of just processed value with keeping of cursor position 769 */ 770 formatQuick: function () { 771 var settingsClone = this.settingsClone, 772 parts = this.getBeforeAfterStriped(), 773 leftLength = this.value;
774 if ((settingsClone.aSep === '' || (settingsClone.aSep !== '' && leftLength.indexOf(settingsClone.aSep) === -1)) && (settingsClone.aSign === '' || (settingsClone.aSign !== '' && leftLength.indexOf(settingsClone.aSign) === -1))) { 775 var subParts = [], 776 nSign = ''; 777 subParts = leftLength.split(settingsClone.aDec); 778 if (subParts[0].indexOf('-') > -1) { 779 nSign = '-'; 780 subParts[0] = subParts[0].replace('-', ''); 781 parts[0] = parts[0].replace('-', ''); 782 } 783 if (subParts[0].length > settingsClone.mInt && parts[0].charAt(0) === '0') { /** strip leading zero if need */ 784 parts[0] = parts[0].slice(1); 785 } 786 parts[0] = nSign + parts[0]; 787 } 788 var value = autoGroup(this.value, this.settingsClone), 789 position = value.length; 790 if (value) { 791 /** prepare regexp which searches for cursor position from unformatted left part */ 792 var left_ar = parts[0].split(''), 793 i = 0; 794 for (i; i < left_ar.length; i += 1) { /** thanks Peter Kovari */ 795 if (!left_ar[i].match('\\d')) { 796 left_ar[i] = '\\' + left_ar[i]; 797 } 798 } 799 var leftReg = new RegExp('^.*?' + left_ar.join('.*?')); 800 /** search cursor position in formatted value */ 801 var newLeft = value.match(leftReg); 802 if (newLeft) { 803 position = newLeft[0].length; 804 /** if we are just before sign which is in prefix position */ 805 if (((position === 0 && value.charAt(0) !== settingsClone.aNeg) || (position === 1 && value.charAt(0) === settingsClone.aNeg)) && settingsClone.aSign && settingsClone.pSign === 'p') { 806 /** place caret after prefix sign */ 807 position = this.settingsClone.aSign.length + (value.charAt(0) === '-' ? 1 : 0); 808 } 809 } else if (settingsClone.aSign && settingsClone.pSign === 's') { 810 /** if we could not find a place for cursor and have a sign as a suffix */ 811 /** place carret before suffix currency sign */ 812 position -= settingsClone.aSign.length; 813 } 814 } 815 this.that.value = value; 816 this.setPosition(position); 817 this.formatted = true; 818 } 819 }; 820 /** thanks to Anthony & Evan C */ 821 function autoGet(obj) { 822 if (typeof obj === 'string') { 823 obj = obj.replace(/\[/g, "\\[").replace(/\]/g, "\\]"); 824 obj = '#' + obj.replace(/(:|\.)/g, '\\$1'); 825 /** obj = '#' + obj.replace(/([;&,\.\+\*\~':"\!\^#$%@\[\]\(\)=>\|])/g, '\\$1'); */ 826 /** possible modification to replace the above 2 lines */ 827 } 828 return $(obj); 829 } 830 831 function getHolder($that, settings, update) { 832 var data = $that.data('autoNumeric'); 833 if (!data) { 834 data = {}; 835 $that.data('autoNumeric', data); 836 } 837 var holder = data.holder; 838 if ((holder === undefined && settings) || update) { 839 holder = new AutoNumericHolder($that.get(0), settings); 840 data.holder = holder; 841 } 842 return holder; 843 } 844 var methods = { 845 init: function (options) { 846 return this.each(function () { 847 var $this = $(this), 848 settings = $this.data('autoNumeric'), /** attempt to grab 'autoNumeric' settings, if they don't exist returns "undefined". */ 849 tagData = $this.data(); /** attempt to grab HTML5 data, if they don't exist we'll get "undefined".*/ 850 if (typeof settings !== 'object') { /** If we couldn't grab settings, create them from defaults and passed options. */ 851 var defaults = { 852 /** allowed numeric values 853 * please do not modify 854 */ 855 aNum: '0123456789', 856 /** allowed thousand separator characters 857 * comma = ',' 858 * period "full stop" = '.' 859 * apostrophe is escaped = '\'' 860 * space = ' ' 861 * none = '' 862 * NOTE: do not use numeric characters 863 */ 864 aSep: ',', 865 /** digital grouping for the thousand separator used in Format 866 * dGroup: '2', results in 99,99,99,999 common in India for values less than 1 billion and greater than -1 billion 867 * dGroup: '3', results in 999,999,999 default 868 * dGroup: '4', results in 9999,9999,9999 used in some Asian countries 869 */ 870 dGroup: '3', 871 /** allowed decimal separator characters 872 * period "full stop" = '.' 873 * comma = ',' 874 */ 875 aDec: '.', 876 /** allow to declare alternative decimal separator which is automatically replaced by aDec
877 * developed for countries the use a comma ',' as the decimal character 878 * and have keyboards\numeric pads that have a period 'full stop' as the decimal characters (Spain is an example) 879 */ 880 altDec: null, 881 /** allowed currency symbol 882 * Must be in quotes aSign: '$', a space is allowed aSign: '$ ' 883 */ 884 aSign: '', 885 /** placement of currency sign 886 * for prefix pSign: 'p', 887 * for suffix pSign: 's', 888 */ 889 pSign: 'p', 890 /** maximum possible value 891 * value must be enclosed in quotes and use the period for the decimal point 892 * value must be larger than vMin 893 */ 894 vMax: '9999999999999.99', 895 /** minimum possible value 896 * value must be enclosed in quotes and use the period for the decimal point 897 * value must be smaller than vMax 898 */ 899 vMin: '0.00', 900 /** max number of decimal places = used to override decimal places set by the vMin & vMax values 901 * value must be enclosed in quotes example mDec: '3', 902 * This can also set the value via a call back function mDec: 'css:# 903 */ 904 mDec: null, 905 /** method used for rounding 906 * mRound: 'S', Round-Half-Up Symmetric (default) 907 * mRound: 'A', Round-Half-Up Asymmetric 908 * mRound: 's', Round-Half-Down Symmetric (lower case s) 909 * mRound: 'a', Round-Half-Down Asymmetric (lower case a) 910 * mRound: 'B', Round-Half-Even "Bankers Rounding" 911 * mRound: 'U', Round Up "Round-Away-From-Zero" 912 * mRound: 'D', Round Down "Round-Toward-Zero" - same as truncate 913 * mRound: 'C', Round to Ceiling "Toward Positive Infinity" 914 * mRound: 'F', Round to Floor "Toward Negative Infinity" 915 */ 916 mRound: 'S', 917 /** controls decimal padding 918 * aPad: true - always Pad decimals with zeros 919 * aPad: false - does not pad with zeros. 920 * aPad: `some number` - pad decimals with zero to number different from mDec 921 * thanks to Jonas Johansson for the suggestion 922 */ 923 aPad: true, 924 /** places brackets on negative value -$ 999.99 to (999.99) 925 * visible only when the field does NOT have focus the left and right symbols should be enclosed in quotes and seperated by a comma 926 * nBracket: null, nBracket: '(,)', nBracket: '[,]', nBracket: '<,>' or nBracket: '{,}' 927 */ 928 nBracket: null, 929 /** Displayed on empty string 930 * wEmpty: 'empty', - input can be blank 931 * wEmpty: 'zero', - displays zero 932 * wEmpty: 'sign', - displays the currency sign 933 */ 934 wEmpty: 'empty', 935 /** controls leading zero behavior 936 * lZero: 'allow', - allows leading zeros to be entered. Zeros will be truncated when entering additional digits. On focusout zeros will be deleted. 937 * lZero: 'deny', - allows only one leading zero on values less than one 938 * lZero: 'keep', - allows leading zeros to be entered. on fousout zeros will be retained. 939 */ 940 lZero: 'allow', 941 /** determine if the default value will be formatted on page ready. 942 * true = automatically formats the default value on page ready 943 * false = will not format the default value 944 */ 945 aForm: true, 946 /** future use */ 947 onSomeEvent: function () {} 948 }; 949 settings = $.extend({}, defaults, tagData, options); /** Merge defaults, tagData and options */ 950 if (settings.aDec === settings.aSep) { 951 $.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character"); 952 return this; 953 } 954 $this.data('autoNumeric', settings); /** Save our new settings */ 955 } else { 956 return this; 957 } 958 settings.runOnce = false;
959 var holder = getHolder($this, settings); 960 if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) === -1 && $this.prop('tagName').toLowerCase() !== 'input') { 961 $.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()"); 962 return this; 963 } 964 if (settings.runOnce === false && settings.aForm) {/** routine to format default value on page load */ 965 if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { 966 var setValue = true; 967 if ($this[0].value === '' && settings.wEmpty === 'empty') { 968 $this[0].value = ''; 969 setValue = false; 970 } 971 if ($this[0].value === '' && settings.wEmpty === 'sign') { 972 $this[0].value = settings.aSign; 973 setValue = false; 974 } 975 if (setValue) { 976 $this.autoNumeric('set', $this.val()); 977 } 978 } 979 if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1 && $this.text() !== '') { 980 $this.autoNumeric('set', $this.text()); 981 } 982 } 983 settings.runOnce = true; 984 if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */ 985 $this.on('keydown.autoNumeric', function (e) { 986 holder = getHolder($this); 987 if (holder.settings.aDec === holder.settings.aSep) { 988 $.error("autoNumeric will not function properly when the decimal character aDec: '" + holder.settings.aDec + "' and thousand separator aSep: '" + holder.settings.aSep + "' are the same character"); 989 return this; 990 } 991 if (holder.that.readOnly) { 992 holder.processed = true; 993 return true; 994 } 995 /** The below streamed code / comment allows the "enter" keydown to throw a change() event */ 996 /** if (e.keyCode === 13 && holder.inVal !== $this.val()){ 997 $this.change(); 998 holder.inVal = $this.val(); 999 }*/ 1000 holder.init(e); 1001 holder.settings.oEvent = 'keydown'; 1002 if (holder.skipAllways(e)) { 1003 holder.processed = true; 1004 return true; 1005 } 1006 if (holder.processAllways()) { 1007 holder.processed = true; 1008 holder.formatQuick(); 1009 e.preventDefault(); 1010 return false; 1011 } 1012 holder.formatted = false; 1013 return true; 1014 }); 1015 $this.on('keypress.autoNumeric', function (e) { 1016 var holder = getHolder($this), 1017 processed = holder.processed; 1018 holder.init(e); 1019 holder.settings.oEvent = 'keypress'; 1020 if (holder.skipAllways(e)) { 1021 return true; 1022 } 1023 if (processed) { 1024 e.preventDefault(); 1025 return false; 1026 } 1027 if (holder.processAllways() || holder.processKeypress()) { 1028 holder.formatQuick(); 1029 e.preventDefault(); 1030 return false; 1031 } 1032 holder.formatted = false;
1033 }); 1034 $this.on('keyup.autoNumeric', function (e) { 1035 var holder = getHolder($this); 1036 holder.init(e); 1037 holder.settings.oEvent = 'keyup'; 1038 var skip = holder.skipAllways(e); 1039 holder.kdCode = 0; 1040 delete holder.valuePartsBeforePaste; 1041 if ($this[0].value === holder.settings.aSign) { /** added to properly place the caret when only the currency is present */ 1042 if (holder.settings.pSign === 's') { 1043 setElementSelection(this, 0, 0); 1044 } else { 1045 setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length); 1046 } 1047 } 1048 if (skip) { 1049 return true; 1050 } 1051 if (this.value === '') { 1052 return true; 1053 } 1054 if (!holder.formatted) { 1055 holder.formatQuick(); 1056 } 1057 }); 1058 $this.on('focusin.autoNumeric', function () { 1059 var holder = getHolder($this); 1060 holder.settingsClone.oEvent = 'focusin'; 1061 if (holder.settingsClone.nBracket !== null) { 1062 var checkVal = $this.val(); 1063 $this.val(negativeBracket(checkVal, holder.settingsClone.nBracket, holder.settingsClone.oEvent)); 1064 } 1065 holder.inVal = $this.val(); 1066 var onempty = checkEmpty(holder.inVal, holder.settingsClone, true); 1067 if (onempty !== null) { 1068 $this.val(onempty); 1069 if (holder.settings.pSign === 's') { 1070 setElementSelection(this, 0, 0); 1071 } else { 1072 setElementSelection(this, holder.settings.aSign.length, holder.settings.aSign.length); 1073 } 1074 } 1075 }); 1076 $this.on('focusout.autoNumeric', function () { 1077 var holder = getHolder($this), 1078 settingsClone = holder.settingsClone, 1079 value = $this.val(), 1080 origValue = value; 1081 holder.settingsClone.oEvent = 'focusout'; 1082 var strip_zero = ''; /** added to control leading zero */ 1083 if (settingsClone.lZero === 'allow') { /** added to control leading zero */ 1084 settingsClone.allowLeading = false; 1085 strip_zero = 'leading'; 1086 } 1087 if (value !== '') { 1088 value = autoStrip(value, settingsClone, strip_zero); 1089 if (checkEmpty(value, settingsClone) === null && autoCheck(value, settingsClone, $this[0])) { 1090 value = fixNumber(value, settingsClone.aDec, settingsClone.aNeg); 1091 value = autoRound(value, settingsClone); 1092 value = presentNumber(value, settingsClone.aDec, settingsClone.aNeg); 1093 } else { 1094 value = ''; 1095 } 1096 } 1097 var groupedValue = checkEmpty(value, settingsClone, false); 1098 if (groupedValue === null) { 1099 groupedValue = autoGroup(value, settingsClone); 1100 } 1101 if (groupedValue !== origValue) { 1102 $this.val(groupedValue); 1103 } 1104 if (groupedValue !== holder.inVal) { 1105 $this.change(); 1106 delete holder.inVal; 1107 } 1108 if (settingsClone.nBracket !== null && $this.autoNumeric('get') < 0) { 1109 holder.settingsClone.oEvent = 'focusout'; 1110 $this.val(negativeBracket($this.val(), settingsClone.nBracket, settingsClone.oEvent)); 1111 } 1112 }); 1113 } 1114 }); 1115 }, 1116 /** method to remove settings and stop autoNumeric() */ 1117 destroy: function () { 1118 return $(this).each(function () { 1119 var $this = $(this); 1120 $this.off('.autoNumeric'); 1121 $this.removeData('autoNumeric'); 1122 }); 1123 }, 1124 /** method to update settings - can call as many times */ 1125 update: function (options) { 1126 return $(this).each(function () { 1127 var $this = autoGet($(this)), 1128 settings = $this.data('autoNumeric'); 1129 if (typeof settings !== 'object') { 1130 $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'update' method"); 1131 return this; 1132 } 1133 var strip = $this.autoNumeric('get'); 1134 settings = $.extend(settings, options); 1135 getHolder($this, settings, true); 1136 if (settings.aDec === settings.aSep) { 1137 $.error("autoNumeric will not function properly when the decimal character aDec: '" + settings.aDec + "' and thousand separator aSep: '" + settings.aSep + "' are the same character"); 1138 return this; 1139 } 1140 $this.data('autoNumeric', settings); 1141 if ($this.val() !== '' || $this.text() !== '') { 1142 return $this.autoNumeric('set', strip); 1143 } 1144 return; 1145 }); 1146 }, 1147 /** returns a formatted strings for "input:text" fields Uses jQuery's .val() method*/ 1148 set: function (valueIn) { 1149 return $(this).each(function () { 1150 var $this = autoGet($(this)), 1151 settings = $this.data('autoNumeric'), 1152 value = valueIn.toString(), 1153 testValue = valueIn.toString(); 1154 if (typeof settings !== 'object') { 1155 $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'set' method"); 1156 return this; 1157 } 1158 /** routine to handle page re-load from back button */ 1159 if (testValue !== $this.attr('value') && $this.prop('tagName').toLowerCase() === 'input' && settings.runOnce === false) { 1160 value = (settings.nBracket !== null) ? negativeBracket($this.val(), settings.nBracket, 'pageLoad') : value; 1161 value = autoStrip(value, settings); 1162 } 1163 /** allows locale decimal separator to be a comma */ 1164 if ((testValue === $this.attr('value') || testValue === $this.text()) && settings.runOnce === false) { 1165 value = value.replace(',', '.'); 1166 } 1167 /** returns a empty string if the value being 'set' contains
1167non-numeric characters and or more than decimal point (full stop) and will not be formatted */ 1168 if (!$.isNumeric(+value)) { 1169 return ''; 1170 } 1171 value = checkValue(value, settings); 1172 settings.oEvent = 'set'; 1173 value.toString(); 1174 if (value !== '') { 1175 value = autoRound(value, settings); 1176 } 1177 value = presentNumber(value, settings.aDec, settings.aNeg); 1178 if (!autoCheck(value, settings)) { 1179 value = autoRound('', settings); 1180 } 1181 value = autoGroup(value, settings); 1182 if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */ 1183 return $this.val(value); 1184 } 1185 if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) { 1186 return $this.text(value); 1187 } 1188 $.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()"); 1189 return false; 1190 }); 1191 }, 1192 /** method to get the unformatted value from a specific input field, returns a numeric value */ 1193 get: function () { 1194 var $this = autoGet($(this)), 1195 settings = $this.data('autoNumeric'); 1196 if (typeof settings !== 'object') { 1197 $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'get' method"); 1198 return this; 1199 } 1200 settings.oEvent = 'get'; 1201 var getValue = ''; 1202 /** determine the element type then use .eq(0) selector to grab the value of the first element in selector */ 1203 if ($this.is('input[type=text], input[type=hidden], input[type=tel], input:not([type])')) { /**added hidden type */ 1204 getValue = $this.eq(0).val(); 1205 } else if ($.inArray($this.prop('tagName').toLowerCase(), settings.tagList) !== -1) { 1206 getValue = $this.eq(0).text(); 1207 } else { 1208 $.error("The <" + $this.prop('tagName').toLowerCase() + "> is not supported by autoNumeric()"); 1209 return false; 1210 } 1211 if ((getValue === '' && settings.wEmpty === 'empty') || (getValue === settings.aSign && (settings.wEmpty === 'sign' || settings.wEmpty === 'empty'))) { 1212 return ''; 1213 } 1214 if (settings.nBracket !== null && getValue !== '') { 1215 getValue = negativeBracket(getValue, settings.nBracket, settings.oEvent); 1216 } 1217 if (settings.runOnce || settings.aForm === false) { 1218 getValue = autoStrip(getValue, settings); 1219 } 1220 getValue = fixNumber(getValue, settings.aDec, settings.aNeg); 1221 if (+getValue === 0 && settings.lZero !== 'keep') { 1222 getValue = '0'; 1223 } 1224 if (settings.lZero === 'keep') { 1225 return getValue; 1226 } 1227 getValue = checkValue(getValue, settings); 1228 return getValue; /** returned Numeric String */ 1229 }, 1230 /** method to get the unformatted value from multiple fields */ 1231 getString: function () { 1232 var isAutoNumeric = false, 1233 $this = autoGet($(this)), 1234 str = $this.serialize(), 1235 parts = str.split('&'), 1236 i = 0; 1237 for (i; i < parts.length; i += 1) { 1238 var miniParts = parts[i].split('='); 1239 var settings = $('*[name="' + decodeURIComponent(miniParts[0]) + '"]').data('autoNumeric'); 1240 if (typeof settings === 'object') { 1241 if (miniParts[1] !== null && $('*[name="' + decodeURIComponent(miniParts[0]) + '"]').data('autoNumeric') !== undefined) { 1242 miniParts[1] = $('input[name="' + decodeURIComponent(miniParts[0]) + '"]').autoNumeric('get'); 1243 parts[i] = miniParts.join('='); 1244 isAutoNumeric = true; 1245 } 1246 } 1247 } 1248 if (isAutoNumeric === true) { 1249 return parts.join('&'); 1250 } 1251 return str; 1252 }, 1253 /** method to get the unformatted value from multiple fields */ 1254 getArray: function () { 1255 var isAutoNumeric = false, 1256 $this = autoGet($(this)), 1257 formFields = $this.serializeArray(); 1258 $.each(formFields, function (i, field) { 1259 var settings = $('*[name="' + decodeURIComponent(field.name) + '"]').data('autoNumeric'); 1260 if (typeof settings === 'object') { 1261 if (field.value !== '' && $('*[name="' + decodeURIComponent(field.name) + '"]').data('autoNumeric') !== undefined) { 1262 field.value = $('input[name="' + decodeURIComponent(field.name) + '"]').autoNumeric('get').toString(); 1263 } 1264 isAutoNumeric = true; 1265 } 1266 }); 1267 if (isAutoNumeric === true) { 1268 return formFields; 1269 } 1270 $.error("You must initialize autoNumeric('init', {options}) prior to calling the 'getArray' method"); 1271 return this; 1272 }, 1273 /** returns the settings object for those who need to look under the hood */ 1274 getSettings: function () { 1275 var $this = autoGet($(this)); 1276 return $this.eq(0).data('autoNumeric'); 1277 } 1278 }; 1279 $.fn.autoNumeric = function (method) { 1280 if (methods[method]) { 1281 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 1282 } 1283 if (typeof method === 'object' || !method) { 1284 return methods.init.apply(this, arguments); 1285 } 1286 $.error('Method "' + method + '" is not supported by autoNumeric()'); 1287 }; 1288}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.