1/** 2 * angular-mask 3 * Personalized input masks for AngularJS 4 * @version v1.2.3 5 * @link http://github.com/assisrafael/angular-input-masks 6 * @license MIT 7 */ 8(function (angular) { 9 10var StringMask = (function() { 11 var tokens = { 12 '0': {pattern: /\d/, default: '0'}, 13 '9': {pattern: /\d/, optional: true}, 14 '#': {pattern: /\d/, optional: true, recursive: true}, 15 'S': {pattern: /[a-zA-Z]/}, 16 '$': {escape: true} 17 }; 18 var isEscaped = function(pattern, pos) { 19 var count = 0; 20 var i = pos - 1; 21 var token = {escape: true}; 22 while (i >= 0 && token && token.escape) { 23 token = tokens[pattern.charAt(i)]; 24 count += token && token.escape ? 1 : 0; 25 i--; 26 } 27 return count > 0 && count%2 === 1; 28 }; 29 var calcOptionalNumbersToUse = function(pattern, value) { 30 var numbersInP = pattern.replace(/[^0]/g,'').length; 31 var numbersInV = value.replace(/[^\d]/g,'').length; 32 return numbersInV - numbersInP; 33 }; 34 var concatChar = function(text, character, options) { 35 if (options.reverse) return character + text; 36 return text
36+ character; 37 }; 38 var hasMoreTokens = function(pattern, pos, inc) { 39 var pc = pattern.charAt(pos); 40 var token = tokens[pc]; 41 if (pc === '') return false; 42 return token && !token.escape ? true : hasMoreTokens(pattern, pos + inc, inc); 43 }; 44 var insertChar = function(text, char, position) { 45 var t = text.split(''); 46 t.splice(position >= 0 ? position: 0, 0, char); 47 return t.join(''); 48 }; 49 var StringMask = function(pattern, opt) { 50 this.options = opt || {}; 51 this.options = { 52 reverse: this.options.reverse || false, 53 usedefaults: this.options.usedefaults || this.options.reverse 54 }; 55 this.pattern = pattern; 56 57 StringMask.prototype.process = function proccess(value) { 58 if (!value) return ''; 59 value = value + ''; 60 var pattern2 = this.pattern; 61 var valid = true; 62 var formatted = ''; 63 var valuePos = this.options.reverse ? value.length - 1 : 0; 64 var optionalNumbersToUse = calcOptionalNumbersToUse(pattern2, value); 65 var escapeNext = false; 66 var recursive = []; 67 var inRecursiveMode = false; 68 69 var steps = { 70 start: this.options.reverse ? pattern2.length - 1 : 0, 71 end: this.options.reverse ? -1 : pattern2.length, 72 inc: this.options.reverse ? -1 : 1 73 }; 74 75 var continueCondition = function(options) { 76 if (!inRecursiveMode && hasMoreTokens(pattern2, i, steps.inc)) { 77 return true; 78 } else if (!inRecursiveMode) { 79 inRecursiveMode = recursive.length > 0; 80 } 81 82 if (inRecursiveMode) { 83 var pc = recursive.shift(); 84 recursive.push(pc); 85 if (options.reverse && valuePos >= 0) { 86 i++; 87 pattern2 = insertChar(pattern2, pc, i); 88 return true; 89 } else if (!options.reverse && valuePos < value.length) { 90 pattern2 = insertChar(pattern2, pc, i); 91 return true; 92 } 93 } 94 return i < pattern2.length && i >= 0; 95 }; 96 97 for (var i = steps.start; continueCondition(this.options); i = i + steps.inc) { 98 var pc = pattern2.charAt(i); 99 var vc = value.charAt(valuePos); 100 var token = tokens[pc]; 101 if (!inRecursiveMode || vc) { 102 if (this.options.reverse && isEscaped(pattern2, i)) { 103 formatted = concatChar(formatted, pc, this.options); 104 i = i + steps.inc; 105 continue; 106 } else if (!this.options.reverse && escapeNext) { 107 formatted = concatChar(formatted, pc, this.options); 108 escapeNext = false; 109 continue; 110 } else if (!this.options.reverse && token && token.escape) { 111 escapeNext = true; 112 continue; 113 } 114 } 115 116 if (!inRecursiveMode && token && token.recursive) { 117 recursive.push(pc); 118 } else if (inRecursiveMode && !vc) { 119 if (!token || !token.recursive) formatted = concatChar(formatted, pc, this.options); 120 continue; 121 } else if (recursive.length > 0 && token && !token.recursive) { 122 // Recursive tokens most be the last tokens of the pattern 123 valid = false; 124 continue; 125 } else if (!inRecursiveMode && recursive.length > 0 && !vc) { 126 continue; 127 } 128 129 if (!token) { 130 formatted = concatChar(formatted, pc, this.options); 131 if (!inRecursiveMode && recursive.length) { 132 recursive.push(pc); 133 } 134 } else if (token.optional) { 135 if (token.pattern.test(vc) && optionalNumbersToUse) { 136 formatted = concatChar(formatted, vc, this.options);
137 valuePos = valuePos + steps.inc; 138 optionalNumbersToUse--; 139 } else if (recursive.length > 0 && vc) { 140 valid = false; 141 break; 142 } 143 } else if (token.pattern.test(vc)) { 144 formatted = concatChar(formatted, vc, this.options); 145 valuePos = valuePos + steps.inc; 146 } else if (!vc && token.default && this.options.usedefaults) { 147 formatted = concatChar(formatted, token.default, this.options); 148 } else { 149 valid = false; 150 break; 151 } 152 } 153 154 return {result: formatted, valid: valid}; 155 }; 156 157 StringMask.prototype.apply = function(value) { 158 return this.process(value).result; 159 }; 160 161 StringMask.prototype.validate = function(value) { 162 return this.process(value).valid; 163 }; 164 }; 165 166 StringMask.process = function(value, pattern, options) { 167 return new StringMask(pattern, options).process(value); 168 }; 169 170 StringMask.apply = function(value, pattern, options) { 171 return new StringMask(pattern, options).apply(value); 172 }; 173 174 StringMask.validate = function(value, pattern, options) { 175 return new StringMask(pattern, options).validate(value); 176 }; 177 178 return StringMask; 179}()); 180 181/** Used to determine if values are of the language type Object */ 182var objectTypes = { 183 'boolean': false, 184 'function': true, 185 'object': true, 186 'number': false, 187 'string': false, 188 'undefined': false 189}; 190 191if (objectTypes[typeof module]) { 192 module.exports = StringMask; 193} 194 195/** 196 * br-validations 197 * A library of validations applicable to several Brazilian data like I.E., CNPJ, CPF and others 198 * @version v0.2.1 199 * @link http://github.com/the-darc/br-validations 200 * @license MIT 201 */ 202(function () { 203 var root = this; 204var CNPJ = {}; 205 206CNPJ.validate = function(c) { 207 var b = [6,5,4,3,2,9,8,7,6,5,4,3,2]; 208 c = c.replace(/[^\d]/g,'').split(''); 209 if(c.length !== 14) { 210 return false; 211 } 212 213 for (var i = 0, n = 0; i < 12; i++) { 214 n += c[i] * b[i+1]; 215 } 216 n = 11 - n%11; 217 n = n >= 10 ? 0 : n; 218 if (parseInt(c[12]) !== n) { 219 return false; 220 } 221 222 for (i = 0, n = 0; i <= 12; i++) { 223 n += c[i] * b[i]; 224 } 225 n = 11 - n%11; 226 n = n >= 10 ? 0 : n; 227 if (parseInt(c[13]) !== n) { 228 return false; 229 } 230 return true; 231}; 232 233 234var CPF = {}; 235 236CPF.validate = function(cpf) { 237 cpf = cpf.replace(/[^\d]+/g,''); 238 if (cpf === '' || cpf === '00000000000' || cpf.length !== 11) { 239 return false; 240 } 241 function validateDigit(digit) { 242 var add = 0; 243 var init = digit - 9; 244 for (var i = 0; i < 9; i ++) { 245 add += parseInt(cpf.charAt(i + init)) * (i+1); 246 } 247 return (add%11)%10 === parseInt(cpf.charAt(digit)); 248 } 249 return validateDigit(9) && validateDigit(10); 250}; 251 252var IE = function(uf) { 253 if (!(this instanceof IE)) { 254 return new IE(uf); 255 } 256 257 this.rules = IErules[uf] || []; 258 this.rule; 259 IE.prototype._defineRule = function(value) { 260 this.rule = undefined; 261 for (var r = 0; r < this.rules.length && this.rule === undefined; r++) { 262 var str = value.replace(/[^\d]/g,''); 263 var ruleCandidate = this.rules[r]; 264 if (str.length === ruleCandidate.chars && (!ruleCandidate.match || ruleCandidate.match.test(value))) { 265 this.rule = ruleCandidate; 266 } 267 } 268 return !!this.rule; 269 }; 270 271 IE.prototype.validate = function(value) { 272 if (!value || !this._defineRule(value)) { 273 return false; 274 } 275 return this.rule.validate(value); 276 }; 277}; 278 279var IErules = {}; 280 281var algorithmSteps = { 282 handleStr: { 283 onlyNumbers: function(str) { 284 return str.replace(/[^\d]/g,'').split(''); 285 }, 286 mgSpec: function(str) { 287 var s = str.replace(/[^\d]/g,''); 288 s = s.substr(0,3)+'0'+s.substr(3, s.length); 289 return s.split(''); 290 } 291 }, 292 sum: { 293 normalSum: function(handledStr, pesos) { 294 var nums = handledStr; 295 var sum = 0; 296 for (var i = 0; i < pesos.length; i++) { 297 sum += parseInt(nums[i]) * pesos[i]; 298 } 299 return sum; 300 }, 301 individualSum: function(handledStr, pesos) { 302 var nums = handledStr; 303 var sum = 0; 304 for (var i = 0; i < pesos.length; i++) { 305 var mult = parseInt(nums[i]) * pesos[i]; 306 sum += mult%10 + parseInt(mult/10); 307 } 308 return sum; 309 }, 310 apSpec: function(handledStr, pesos) { 311 var sum = this.normalSum(handledStr, pesos); 312 var ref = handledStr.join(''); 313 if (ref >= '030000010' && ref <= '030170009') { 314 return sum + 5; 315 } 316 if (ref >= '030170010' && ref <= '030190229') { 317 return sum + 9; 318 } 319 return sum; 320 } 321 }, 322 rest: { 323 mod11: function(sum) { 324 return sum%11; 325 }, 326 mod10: function(sum) { 327 return sum%10; 328 }, 329 mod9: function(sum) { 330 return sum%9; 331 } 332 }, 333 expectedDV: { 334 minusRestOf11: function(rest) { 335 return rest < 2 ? 0 : 11 - rest; 336 }, 337 minusRestOf11v2: function(rest) { 338 return rest < 2 ? 11 - rest - 10 : 11 - rest; 339 }, 340 minusRestOf10: function(rest) { 341 return rest < 1 ? 0 : 10 - rest; 342 }, 343 mod10: function(rest) { 344 return rest%10; 345 }, 346 goSpec: function(rest, handledStr) { 347 var ref = handledStr.join(''); 348 if (rest === 1) { 349 return ref >= '101031050' && ref <= '101199979' ? 1 : 0; 350 } 351 return rest === 0 ? 0 : 11 - rest; 352 }, 353 apSpec: function(rest, handledStr) { 354 var ref = handledStr.join(''); 355 if (rest === 0) { 356 return ref >= '030170010' && ref <= '030190229' ? 1 : 0; 357 } 358 return rest === 1 ? 0 : 11 - rest; 359 }, 360 voidFn: function(rest) { 361 return rest; 362 } 363 } 364}; 365 366 367/** 368 * options { 369 * pesos: Array of values used to operate in sum step 370 * dvPos: Position of the DV to validate considering the handledStr 371 * algorithmSteps: The four DV's validation algorithm steps names 372 * } 373 */ 374function validateDV(value, options) { 375 var steps = options.algorithmSteps; 376 377 // Step 01: Handle String 378 var handledStr = algorithmSteps.handleStr[steps[0]](value); 379 380 // Step 02: Sum chars 381 var sum = algorithmSteps.sum[steps[1]](handledStr, options.pesos); 382 383 // Step 03: Rest calculation 384 var rest = algorithmSteps.rest[steps[2]](sum); 385 386 // Fixed Step: Get current DV 387 var currentDV = parseInt(handledStr[options.dvpos]); 388 389 // Step 04: Expected DV calculation 390 var expectedDV = algorithmSteps.expectedDV[steps[3]](rest, handledStr); 391 392 // Fixed step: DV verification 393 return currentDV === expectedDV; 394} 395 396function validateIE(value, rule) { 397 if (rule.match && !rule.match.test(value)) { 398 return false;
399 } 400 for (var i = 0; i < rule.dvs.length; i++) { 401 // console.log('>> >> dv'+i); 402 if (!validateDV(value, rule.dvs[i])) { 403 return false; 404 } 405 } 406 return true; 407} 408 409IErules.PE = [{ 410 //mask: new StringMask('0000000-00'), 411 chars: 9, 412 dvs: [{ 413 dvpos: 7, 414 pesos: [8,7,6,5,4,3,2], 415 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 416 },{ 417 dvpos: 8, 418 pesos: [9,8,7,6,5,4,3,2], 419 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 420 }], 421 validate: function(value) { return validateIE(value, this); } 422},{ 423 // mask: new StringMask('00.0.000.0000000-0'), 424 chars: 14, 425 pesos: [[1,2,3,4,5,9,8,7,6,5,4,3,2]], 426 dvs: [{ 427 dvpos: 13, 428 pesos: [5,4,3,2,1,9,8,7,6,5,4,3,2], 429 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11v2'] 430 }], 431 validate: function(value) { return validateIE(value, this); } 432}]; 433 434IErules.RS = [{ 435 // mask: new StringMask('000/0000000'), 436 chars: 10, 437 dvs: [{ 438 dvpos: 9, 439 pesos: [2,9,8,7,6,5,4,3,2], 440 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 441 }], 442 validate: function(value) { return validateIE(value, this); } 443}]; 444 445IErules.AC = [{ 446 // mask: new StringMask('00.000.000/000-00'), 447 chars: 13, 448 match: /^01/, 449 dvs: [{ 450 dvpos: 11, 451 pesos: [4,3,2,9,8,7,6,5,4,3,2], 452 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 453 },{ 454 dvpos: 12, 455 pesos: [5,4,3,2,9,8,7,6,5,4,3,2], 456 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 457 }], 458 validate: function(value) { return validateIE(value, this); } 459}]; 460 461IErules.MG = [{ 462 // mask: new StringMask('000.000.000/0000'), 463 chars: 13, 464 dvs: [{ 465 dvpos: 12, 466 pesos: [1,2,1,2,1,2,1,2,1,2,1,2], 467 algorithmSteps: ['mgSpec', 'individualSum', 'mod10', 'minusRestOf10'] 468 },{ 469 dvpos: 12, 470 pesos: [3,2,11,10,9,8,7,6,5,4,3,2], 471 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 472 }], 473 validate: function(value) { return validateIE(value, this); } 474}]; 475 476IErules.SP = [{ 477 // mask: new StringMask('000.000.000.000'), 478 chars: 12, 479 match: /^[0-9]/, 480 dvs: [{ 481 dvpos: 8, 482 pesos: [1,3,4,5,6,7,8,10], 483 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'mod10'] 484 },{ 485 dvpos: 11, 486 pesos: [3,2,10,9,8,7,6,5,4,3,2], 487 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'mod10'] 488 }], 489 validate: function(value) { return validateIE(value, this); } 490},{ 491 // mask: new StringMask('P-00000000.0/000') 492 chars: 12, 493 match: /^P/i, 494 dvs: [{ 495 dvpos: 8, 496 pesos: [1,3,4,5,6,7,8,10], 497 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'mod10'] 498 }], 499 validate: function(value) { return validateIE(value, this); } 500}]; 501 502IErules.DF = [{ 503 // mask: new StringMask('00000000000-00'), 504 chars: 13, 505 dvs: [{ 506 dvpos: 11, 507 pesos: [4,3,2,9,8,7,6,5,4,3,2], 508 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 509 },{ 510 dvpos: 12, 511 pesos: [5,4,3,2,9,8,7,6,5,4,3,2], 512 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 513 }], 514 validate: function(value) { return validateIE(value, this); } 515}]; 516 517IErules.ES = [{ 518 // mask: new StringMask('000.000.00-0') 519 chars: 9, 520 dvs: [{ 521 dvpos: 8, 522 pesos: [9,8,7,6,5,4,3,2], 523 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 524 }], 525 validate: function(value) { return validateIE(value, this); } 526}]; 527 528IErules.BA = [{ 529 // mask: new StringMask('000000-00') 530 chars: 8, 531 match: /^[0123458]/, 532 dvs: [{ 533 dvpos: 7, 534 pesos: [7,6,5,4,3,2], 535 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod10', 'minusRestOf10'] 536 },{ 537 dvpos: 6, 538 pesos: [8,7,6,5,4,3,0,2], 539 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod10', 'minusRestOf10'] 540 }], 541 validate: function(value) { return validateIE(value, this); } 542},{ 543 chars: 8, 544 match: /^[679]/, 545 dvs: [{ 546 dvpos: 7, 547 pesos: [7,6,5,4,3,2], 548 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 549 },{ 550 dvpos: 6, 551 pesos: [8,7,6,5,4,3,0,2], 552 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 553 }], 554 validate: function(value) { return validateIE(value, this); } 555},{ 556 // mask: new StringMask('0000000-00') 557 chars: 9, 558 match: /^[0-9][0123458]/, 559 dvs: [{ 560 dvpos: 8, 561 pesos: [8,7,6,5,4,3,2], 562 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod10', 'minusRestOf10'] 563 },{ 564 dvpos: 7, 565 pesos: [9,8,7,6,5,4,3,0,2], 566 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod10', 'minusRestOf10'] 567 }], 568 validate: function(value) { return validateIE(value, this); } 569},{ 570 chars: 9, 571 match: /^[0-9][679]/, 572 dvs: [{ 573 dvpos: 8, 574 pesos: [8,7,6,5,4,3,2], 575 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 576 },{ 577 dvpos: 7, 578 pesos: [9,8,7,6,5,4,3,0,2], 579 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 580 }], 581 validate: function(value) { return validateIE(value, this); } 582}]; 583 584IErules.AM = [{ 585 //mask: new StringMask('00.000.000-0') 586 chars: 9, 587 dvs: [{ 588 dvpos: 8, 589 pesos: [9,8,7,6,5,4,3,2], 590 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 591 }], 592 validate: function(value) { return validateIE(value, this); } 593}]; 594 595IErules.RN = [{ 596 // {mask: new StringMask('00.000.000-0') 597 chars: 9, 598 match: /^20/, 599 dvs: [{ 600 dvpos: 8, 601 pesos: [9,8,7,6,5,4,3,2], 602 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 603 }], 604 validate: function(value) { return validateIE(value, this); } 605},{ 606 // {mask: new StringMask('00.0.000.000-0'), chars: 10} 607 chars: 10, 608 match: /^20/, 609 dvs: [{ 610 dvpos: 8, 611 pesos: [10,9,8,7,6,5,4,3,2], 612 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 613 }], 614 validate: function(value) { return validateIE(value, this); } 615}]; 616 617IErules.RO = [{ 618 // mask: new StringMask('0000000000000-0') 619 chars: 14, 620 dvs: [{ 621 dvpos: 13, 622 pesos: [6,5,4,3,2,9,8,7,6,5,4,3,2], 623 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 624 }], 625 validate: function(value) { return validateIE(value, this); } 626}]; 627 628IErules.PR = [{ 629 // mask: new StringMask('00000000-00') 630 chars: 10, 631 dvs: [{ 632 dvpos: 8, 633 pesos: [3,2,7,6,5,4,3,2], 634 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 635 },{ 636 dvpos: 9, 637 pesos: [4,3,2,7,6,5,4,3,2], 638 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 639 }], 640 validate: function(value) { return validateIE(value, this); } 641}]; 642 643IErules.SC = [{ 644 // {mask: new StringMask('000.000.000'), uf: 'SANTA CATARINA'} 645 chars: 9, 646 dvs: [{ 647 dvpos: 8, 648 pesos: [9,8,7,6,5,4,3,2], 649 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 650 }], 651 validate: function(value) { return validateIE(value, this); } 652}]; 653 654IErules.RJ = [{ 655 // {mask: new StringMask('00.000.00-0'), uf: 'RIO DE JANEIRO'} 656 chars: 8, 657 dvs: [{ 658 dvpos: 7, 659 pesos: [2,7,6,5,4,3,2], 660 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 661 }], 662 validate: function(value) { return validateIE(value, this); } 663}]; 664 665IErules.PA = [{ 666 // {mask: new StringMask('00-000000-0') 667 chars: 9, 668 match: /^15/, 669 dvs: [{ 670 dvpos: 8, 671 pesos: [9,8,7,6,5,4,3,2], 672 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 673 }], 674 validate: function(value) { return validateIE(value, this); } 675}]; 676 677IErules.SE = [{ 678 // {mask: new StringMask('00000000-0') 679 chars: 9, 680 dvs: [{ 681 dvpos: 8, 682 pesos: [9,8,7,6,5,4,3,2], 683 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 684 }], 685 validate: function(value) { return validateIE(value, this); } 686}]; 687 688IErules.PB = [{ 689 // {mask: new StringMask('00000000-0') 690 chars: 9, 691 dvs: [{ 692 dvpos: 8, 693 pesos: [9,8,7,6,5,4,3,2], 694 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 695 }], 696 validate: function(value) { return validateIE(value, this); } 697}]; 698
699IErules.CE = [{ 700 // {mask: new StringMask('00000000-0') 701 chars: 9, 702 dvs: [{ 703 dvpos: 8, 704 pesos: [9,8,7,6,5,4,3,2], 705 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 706 }], 707 validate: function(value) { return validateIE(value, this); } 708}]; 709 710IErules.PI = [{ 711 // {mask: new StringMask('000000000') 712 chars: 9, 713 dvs: [{ 714 dvpos: 8, 715 pesos: [9,8,7,6,5,4,3,2], 716 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 717 }], 718 validate: function(value) { return validateIE(value, this); } 719}]; 720 721IErules.MA = [{ 722 // {mask: new StringMask('000000000') 723 chars: 9, 724 match: /^12/, 725 dvs: [{ 726 dvpos: 8, 727 pesos: [9,8,7,6,5,4,3,2], 728 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 729 }], 730 validate: function(value) { return validateIE(value, this); } 731}]; 732 733IErules.MT = [{ 734 // {mask: new StringMask('0000000000-0') 735 chars: 11, 736 dvs: [{ 737 dvpos: 10, 738 pesos: [3,2,9,8,7,6,5,4,3,2], 739 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 740 }], 741 validate: function(value) { return validateIE(value, this); } 742}]; 743 744IErules.MS = [{ 745 // {mask: new StringMask('000000000') 746 chars: 9, 747 match: /^28/, 748 dvs: [{ 749 dvpos: 8, 750 pesos: [9,8,7,6,5,4,3,2], 751 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 752 }], 753 validate: function(value) { return validateIE(value, this); } 754}]; 755 756IErules.TO = [{ 757 // {mask: new StringMask('00000000000'), 758 chars: 11, 759 match: /^[0-9]{2}((0[123])|(99))/, 760 dvs: [{ 761 dvpos: 10, 762 pesos: [9,8,0,0,7,6,5,4,3,2], 763 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 764 }], 765 validate: function(value) { return validateIE(value, this); } 766}]; 767 768IErules.AL = [{ 769 // {mask: new StringMask('000000000') 770 chars: 9, 771 match: /^24[03578]/, 772 dvs: [{ 773 dvpos: 8, 774 pesos: [9,8,7,6,5,4,3,2], 775 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'minusRestOf11'] 776 }], 777 validate: function(value) { return validateIE(value, this); } 778}]; 779 780IErules.RR = [{ 781 // {mask: new StringMask('00000000-0') 782 chars: 9, 783 match: /^24/, 784 dvs: [{ 785 dvpos: 8, 786 pesos: [1,2,3,4,5,6,7,8], 787 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod9', 'voidFn'] 788 }], 789 validate: function(value) { return validateIE(value, this); } 790}]; 791 792IErules.GO = [{ 793 // {mask: new StringMask('00.000.000-0') 794 chars: 9, 795 match: /^1[015]/, 796 dvs: [{ 797 dvpos: 8, 798 pesos: [9,8,7,6,5,4,3,2], 799 algorithmSteps: ['onlyNumbers', 'normalSum', 'mod11', 'goSpec'] 800 }], 801 validate: function(value) { return validateIE(value, this); } 802}]; 803 804IErules.AP = [{ 805 // {mask: new StringMask('000000000') 806 chars: 9, 807 match: /^03/, 808 dvs: [{ 809 dvpos: 8, 810 pesos: [9,8,7,6,5,4,3,2], 811 algorithmSteps: ['onlyNumbers', 'apSpec', 'mod11', 'apSpec'] 812 }], 813 validate: function(value) { return validateIE(value, this); } 814}]; 815 816var BrV = { 817 ie: IE, 818 cpf: CPF, 819 cnpj: CNPJ 820}; 821var objectTypes = { 822 'function': true, 823 'object': true 824}; 825if (objectTypes[typeof module]) { 826 module.exports = BrV; 827} else { 828 root.BrV = BrV; 829} 830}.call(this)); 831(function() { 832 'use strict'; 833 834 function maxValidator(ctrl, value, limit) { 835 var max = parseFloat(limit); 836 var validity = ctrl.$isEmpty(value) || isNaN(max)|| value <= max; 837 ctrl.$setValidity('max', validity); 838 return value; 839 } 840 841 function minValidator(ctrl, value, limit) { 842 var min = parseFloat(limit); 843 var validity = ctrl.$isEmpty(value) || isNaN(min) || value >= min; 844 ctrl.$setValidity('min', validity); 845 return value; 846 } 847 848 var cnpjPattern = new StringMask('00.000.000\/0000-00'); 849 var cpfPattern = new StringMask('000.000.000-00'); 850 851 function numberViewMask (decimals, decimalDelimiter, thousandsDelimiter) { 852 var mask = '#' + thousandsDelimiter + '##0'; 853 854 if(decimals > 0) { 855 mask += decimalDelimiter; 856 for (var i = 0; i < decimals; i++) { 857 mask += '0'; 858 } 859 } 860 861 return new StringMask(mask, { 862 reverse:true 863 }); 864 } 865 866 function numberModelMask (decimals) { 867 var mask = '###0'; 868 869 if(decimals > 0) { 870 mask += '.'; 871 for (var i = 0; i < decimals; i++) { 872 mask += '0'; 873 } 874 } 875 876 return new StringMask(mask, { 877 reverse:true 878 }); 879 } 880 881 function clearDelimitersAndLeadingZeros (value) { 882 var cleanValue = value.replace(/^0*/, ''); 883 cleanValue = cleanValue.replace(/[^0-9]/g, ''); 884 return cleanValue; 885 } 886 887 function preparePercentageToFormatter (value, decimals) { 888 return clearDelimitersAndLeadingZeros((parseFloat(value)*100).toFixed(decimals)); 889 } 890 891 function prepareNumberToFormatter (value, decimals) { 892 return clearDelimitersAndLeadingZeros((parseFloat(value)).toFixed(decimals)); 893 } 894 895 function validateBrPhoneNumber (ctrl, value) {
896 var valid = ctrl.$isEmpty(value) || value.length === 8 || value.length === 9; 897 ctrl.$setValidity('br-phone-number', valid); 898 return value; 899 } 900 901 function validateCPF (ctrl, value) { 902 var valid = ctrl.$isEmpty(value) || BrV.cpf.validate(value); 903 ctrl.$setValidity('cpf', valid); 904 return value; 905 } 906 907 function validateCNPJ (ctrl, value) { 908 var valid = ctrl.$isEmpty(value) || BrV.cnpj.validate(value); 909 ctrl.$setValidity('cnpj', valid); 910 return value; 911 } 912 913 function validateCPForCNPJ (ctrl, value) { 914 if(!value || value.length <= 11) { 915 validateCNPJ(ctrl, ''); 916 return validateCPF(ctrl, value); 917 }else { 918 validateCPF(ctrl, ''); 919 return validateCNPJ(ctrl, value); 920 } 921 } 922 923 function uiBrCpfMask() { 924 function applyCpfMask (value) { 925 if(!value) { 926 return value; 927 } 928 var formatedValue = cpfPattern.apply(value); 929 return formatedValue.trim().replace(/[^0-9]$/, ''); 930 } 931 932 return { 933 restrict: 'A', 934 require: '?ngModel', 935 link: function (scope, element, attrs, ctrl) { 936 if (!ctrl) { 937 return; 938 } 939 940 ctrl.$formatters.push(function(value) { 941 return applyCpfMask(validateCPF(ctrl, value)); 942 }); 943 944 ctrl.$parsers.push(function(value) { 945 if(!value) { 946 return value; 947 } 948 949 var actualNumber = value.replace(/[^\d]/g,''); 950 var formatedValue = applyCpfMask(actualNumber); 951 952 if (ctrl.$viewValue !== formatedValue) { 953 ctrl.$setViewValue(formatedValue); 954 ctrl.$render(); 955 } 956 957 return formatedValue.replace(/[^\d]+/g,''); 958 }); 959 960 ctrl.$parsers.push(function(value) { 961 return validateCPF(ctrl, value); 962 }); 963 } 964 }; 965 } 966 967 function uiBrCnpjMask() { 968 function applyCnpjMask (value) { 969 if(!value) { 970 return value; 971 } 972 var formatedValue = cnpjPattern.apply(value); 973 return formatedValue.trim().replace(/[^0-9]$/, ''); 974 } 975 return { 976 restrict: 'A', 977 require: '?ngModel', 978 link: function (scope, element, attrs, ctrl) { 979 if (!ctrl) { 980 return; 981 } 982 983 ctrl.$formatters.push(function(value) { 984 return applyCnpjMask(validateCNPJ(ctrl, value)); 985 }); 986 987 ctrl.$parsers.push(function(value) { 988 if(!value) { 989 return value; 990 } 991 992 var actualNumber = value.replace(/[^\d]+/g,''); 993 var formatedValue = applyCnpjMask(actualNumber); 994 995 if (ctrl.$viewValue !== formatedValue) { 996 ctrl.$setViewValue(formatedValue); 997 ctrl.$render(); 998 } 999 1000 return formatedValue.replace(/[^\d]+/g,''); 1001 }); 1002 1003 ctrl.$parsers.push(function(value) { 1004 return validateCNPJ(ctrl, value); 1005 }); 1006 } 1007 }; 1008 } 1009 1010 function uiBrCpfCnpjMask() { 1011 function applyCpfCnpjMask (value) { 1012 if(!value) { 1013 return value; 1014 } 1015 var formatedValue; 1016 if (value.length > 11) { 1017 formatedValue = cnpjPattern.apply(value); 1018 } else { 1019 formatedValue = cpfPattern.apply(value); 1020 } 1021 return formatedValue.trim().replace(/[^0-9]$/, ''); 1022 } 1023 return { 1024 restrict: 'A', 1025 require: '?ngModel', 1026 link: function (scope, element, attrs, ctrl) { 1027 if (!ctrl) { 1028 return; 1029 } 1030 1031 ctrl.$formatters.push(function(value) { 1032 return applyCpfCnpjMask(validateCPForCNPJ(ctrl, value)); 1033 }); 1034 1035 ctrl.$parsers.push(function(value) { 1036 if(!value) { 1037 return value; 1038 } 1039 1040 var actualNumber = value.replace(/[^\d]+/g,''); 1041 var formatedValue = applyCpfCnpjMask(actualNumber); 1042 1043 if (ctrl.$viewValue !== formatedValue) { 1044 ctrl.$setViewValue(formatedValue); 1045 ctrl.$render(); 1046 } 1047 1048 return formatedValue.replace(/[^\d]+/g,''); 1049 }); 1050 1051 ctrl.$parsers.push(function(value) { 1052 return validateCPForCNPJ(ctrl, value); 1053 }); 1054 } 1055 }; 1056 } 1057 1058 angular.module('ui.utils.masks', []) 1059 .directive('uiPercentageMask', ['$locale', '$parse', function ($locale, $parse) { 1060 return { 1061 restrict: 'A', 1062 require: '?ngModel', 1063 link: function (scope, element, attrs, ctrl) { 1064 var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP, 1065 thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP, 1066 decimals = parseInt(attrs.uiPercentageMask); 1067 1068 if (!ctrl) { 1069 return; 1070 } 1071 1072 if (angular.isDefined(attrs.uiHideGroupSep)){ 1073 thousandsDelimiter = ''; 1074 } 1075 1076 if(isNaN(decimals)) { 1077 decimals = 2; 1078 } 1079 var numberDecimals = decimals + 2; 1080 var viewMask = numberViewMask(decimals, decimalDelimiter, thousandsDelimiter), 1081 modelMask = numberModelMask(numberDecimals); 1082 1083 ctrl.$formatters.push(function(value) { 1084 if(!value) { 1085 return value; 1086 } 1087 1088 var valueToFormat = preparePercentageToFormatter(value, decimals); 1089 return viewMask.apply(valueToFormat) + ' %'; 1090 }); 1091 1092 function parse(value) { 1093 if(!value) { 1094 return value; 1095 } 1096 1097 var valueToFormat = clearDelimitersAndLeadingZeros(value) || '0'; 1098 if(value.length > 1 && value.indexOf('%') === -1) { 1099 valueToFormat = valueToFormat.slice(0,valueToFormat.length-1); 1100 } 1101 var formatedValue = viewMask.apply(valueToFormat) + ' %'; 1102 var actualNumber = parseFloat(modelMask.apply(valueToFormat)); 1103 1104 if (ctrl.$viewValue !== formatedValue) { 1105 ctrl.$setViewValue(formatedValue); 1106 ctrl.$render(); 1107 } 1108 1109 return actualNumber; 1110 } 1111 1112 ctrl.$parsers.push(parse); 1113 1114 if (attrs.uiPercentageMask) { 1115 scope.$watch(attrs.uiPercentageMask, function(decimals) { 1116 if(isNaN(decimals)) { 1117 decimals = 2; 1118 } 1119 numberDecimals = decimals + 2; 1120 viewMask = numberViewMask(decimals, decimalDelimiter, thousandsDelimiter); 1121 modelMask = numberModelMask(numberDecimals); 1122 1123 parse(ctrl.$viewValue || ''); 1124 }); 1125 } 1126 1127 if(attrs.min){ 1128 ctrl.$parsers.push(function(value) { 1129 var min = $parse(attrs.min)(scope); 1130 return minValidator(ctrl, value, min); 1131 }); 1132 1133 scope.$watch('min', function(value) { 1134 minValidator(ctrl, ctrl.$modelValue, value); 1135 }); 1136 } 1137 1138 if(attrs.max) { 1139 ctrl.$parsers.push(function(value) { 1140 var max = $parse(attrs.max)(scope); 1141 return maxValidator(ctrl, value, max); 1142 }); 1143 1144 scope.$watch('max', function(value) { 1145 maxValidator(ctrl, ctrl.$modelValue, value); 1146 }); 1147 } 1148 } 1149 }; 1150 }]) 1151 .directive('uiNumberMask', ['$locale', '$parse', function ($locale, $parse) { 1152 return { 1153 restrict: 'A', 1154 require: '?ngModel', 1155 link: function (scope, element, attrs, ctrl) { 1156 var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP, 1157 thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP, 1158 decimals = $parse(attrs.uiNumberMask)(scope); 1159 1160 if (!ctrl) { 1161 return; 1162 } 1163 1164 if (angular.isDefined(attrs.uiHideGroupSep)){ 1165 thousandsDelimiter = ''; 1166 } 1167 1168 if(isNaN(decimals)) { 1169 decimals = 2; 1170 } 1171 var viewMask = numberViewMask(decimals, decimalDelimiter, thousandsDelimiter), 1172 modelMask = numberModelMask(decimals); 1173 1174 function parse(value) { 1175 if(!value) { 1176 return value; 1177 } 1178 1179 var valueToFormat = clearDelimitersAndLeadingZeros(value) || '0'; 1180 var formatedValue = viewMask.apply(valueToFormat); 1181 var actualNumber = parseFloat(modelMask.apply(valueToFormat)); 1182 1183 if(angular.isDefined(attrs.uiNegativeNumber)){ 1184 var isNegative = (value[0] === '-'), 1185 needsToInvertSign = (value.slice(-1) === '-'); 1186 1187 //only apply the minus sign if it is negative or(exclusive) needs to be negative 1188 if(needsToInvertSign ^ isNegative) { 1189 actualNumber *= -1; 1190 formatedValue = '-' + formatedValue; 1191 } 1192 } 1193 1194 if (ctrl.$viewValue !== formatedValue) { 1195 ctrl.$setViewValue(formatedValue); 1196 ctrl.$render(); 1197 } 1198 1199 return actualNumber; 1200 } 1201 1202 ctrl.$formatters.push(function(value) { 1203 var prefix = ''; 1204 if(angular.isDefined(attrs.uiNegativeNumber) && value < 0){ 1205 prefix = '-'; 1206 } 1207 1208 if(!value) { 1209 return value; 1210 } 1211 1212 var valueToFormat = prepareNumberToFormatter(value, decimals); 1213 return prefix + viewMask.apply(valueToFormat); 1214 }); 1215 1216 ctrl.$parsers.push(parse); 1217 1218 if (attrs.uiNumberMask) { 1219 scope.$watch(attrs.uiNumberMask, function(decimals) { 1220 if(isNaN(decimals)) { 1221 decimals = 2; 1222 } 1223 viewMask = numberViewMask(decimals, decimalDelimiter, thousandsDelimiter); 1224 modelMask = numberModelMask(decimals); 1225 1226 parse(ctrl.$viewValue || ''); 1227 }); 1228 } 1229 1230 if(attrs.min){ 1231 ctrl.$parsers.push(function(value) { 1232 var min = $parse(attrs.min)(scope); 1233 return minValidator(ctrl, value, min); 1234 }); 1235 1236 scope.$watch(attrs.min, function(value) { 1237 minValidator(ctrl, ctrl.$modelValue, value); 1238 }); 1239 } 1240 1241 if(attrs.max) { 1242 ctrl.$parsers.push(function(value) { 1243 var max = $parse(attrs.max)(scope); 1244 return maxValidator(ctrl, value, max); 1245 }); 1246 1247 scope.$watch(attrs.max, function(value) { 1248 maxValidator(ctrl, ctrl.$modelValue, value); 1249 }); 1250 } 1251 } 1252 }; 1253 }]) 1254 .directive('uiBrCpfMask', [uiBrCpfMask]) 1255 .directive('uiBrCnpjMask', [uiBrCnpjMask]) 1256 .directive('uiBrCpfcnpjMask', [uiBrCpfCnpjMask]) 1257 // deprecated: will be removed in the next major version 1258 .directive('uiCpfMask', [uiBrCpfMask]) 1259 // deprecated: will be removed in the next major version 1260 .directive('uiCnpjMask', [uiBrCnpjMask]) 1261 // deprecated: will be removed in the next major version 1262 .directive('uiCpfcnpjMask', [uiBrCpfCnpjMask]) 1263 .directive('uiMoneyMask', ['$locale', '$parse', function ($locale, $parse) { 1264 return { 1265 restrict: 'A', 1266 require: '?ngModel', 1267 link: function (scope, element, attrs, ctrl) { 1268 var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP, 1269 thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP, 1270 currencySym = $locale.NUMBER_FORMATS.CURRENCY_SYM, 1271 decimals = parseInt(attrs.uiMoneyMask); 1272 1273 if (!ctrl) { 1274 return; 1275 } 1276 1277 if (angular.isDefined(attrs.uiHideGroupSep)){ 1278 thousandsDelimiter = ''; 1279 } 1280 1281 if(isNaN(decimals)) { 1282 decimals = 2; 1283 } 1284 var decimalsPattern = decimals > 0 ? decimalDelimiter + new Array(decimals + 1).join('0') : ''; 1285 var maskPattern = currencySym+' #'+thousandsDelimiter+'##0'+decimalsPattern; 1286 var moneyMask = new StringMask(maskPattern, {reverse: true}); 1287 1288 ctrl.$formatters.push(function(value) { 1289 if(!value) { 1290 return value; 1291 } 1292 1293 return moneyMask.apply(value.toFixed(decimals).replace(/[^\d]+/g,'')); 1294 }); 1295 1296 function parse(value) { 1297 if (!value) { 1298 return value; 1299 } 1300 1301 var actualNumber = value.replace(/[^\d]+/g,''); 1302 actualNumber = actualNumber.replace(/^[0]+([1-9])/,'$1'); 1303 var formatedValue = moneyMask.apply(actualNumber); 1304 1305 if (value !== formatedValue) { 1306 ctrl.$setViewValue(formatedValue); 1307 ctrl.$render(); 1308 } 1309 1310 return formatedValue ? parseInt(formatedValue.replace(/[^\d]+/g,''))/Math.pow(10,decimals) : null; 1311 } 1312 1313 ctrl.$parsers.push(parse); 1314 1315 if (attrs.uiMoneyMask) { 1316 scope.$watch(attrs.uiMoneyMask, function(decimals) { 1317 if(isNaN(decimals)) { 1318 decimals = 2; 1319 } 1320 decimalsPattern = decimals > 0 ? decimalDelimiter + new Array(decimals + 1).join('0') : ''; 1321 maskPattern = currencySym+' #'+thousandsDelimiter+'##0'+decimalsPattern; 1322 moneyMask = new StringMask(maskPattern, {reverse: true}); 1323 1324 parse(ctrl.$viewValue || ''); 1325 }); 1326 } 1327 1328 if(attrs.min){ 1329 ctrl.$parsers.push(function(value) { 1330 var min = $parse(attrs.min)(scope); 1331 return minValidator(ctrl, value, min); 1332 }); 1333 1334 scope.$watch(attrs.min, function(value) { 1335 minValidator(ctrl, ctrl.$modelValue, value); 1336 }); 1337 } 1338 1339 if(attrs.max) { 1340 ctrl.$parsers.push(function(value) { 1341 var max = $parse(attrs.max)(scope); 1342 return maxValidator(ctrl, value, max); 1343 }); 1344 1345 scope.$watch(attrs.max, function(value) { 1346 maxValidator(ctrl, ctrl.$modelValue, value); 1347 }); 1348 } 1349 } 1350 }; 1351 }]) 1352 .directive('uiBrPhoneNumber',function() { 1353 /** 1354 * FIXME: all numbers will have 9 digits after 2016. 1355 * see http://portal.embratel.com.br/embratel/9-digito/ 1356 */ 1357 var phoneMask8D = new StringMask('0000-0000'), 1358 phoneMask9D = new StringMask('00000-0000'); 1359 1360 function clearValue (value) { 1361 if(!value) { 1362 return value; 1363 } 1364 1365 return value.replace(/[^0-9]/g, ''); 1366 } 1367 1368 function applyPhoneMask (value) { 1369 if(!value) { 1370 return value; 1371 } 1372 1373 var formatedValue; 1374 if(value.length < 9){ 1375 formatedValue = phoneMask8D.apply(value); 1376 }else{ 1377 formatedValue = phoneMask9D.apply(value); 1378 } 1379 1380 return formatedValue.trim().replace(/[^0-9]$/, ''); 1381 } 1382 1383 return { 1384 restrict: 'A', 1385 require: '?ngModel', 1386 link: function(scope, element, attrs, ctrl) { 1387 if (!ctrl) { 1388 return; 1389 } 1390 1391 ctrl.$formatters.push(function(value) { 1392 return applyPhoneMask(validateBrPhoneNumber(ctrl, value)); 1393 }); 1394 1395 ctrl.$parsers.push(function(value) { 1396 if (!value) { 1397 return value; 1398 } 1399 1400 var cleanValue = clearValue(value); 1401 var formatedValue = applyPhoneMask(cleanValue); 1402 1403 if (ctrl.$viewValue !== formatedValue) { 1404 ctrl.$setViewValue(formatedValue); 1405 ctrl.$render(); 1406 } 1407 1408 return clearValue(formatedValue); 1409 }); 1410 1411 ctrl.$parsers.push(function(value) { 1412 return validateBrPhoneNumber(ctrl, value); 1413 }); 1414 } 1415 }; 1416 }) 1417 .directive('uiBrDddPhoneNumber', function () { 1418 /** 1419 * FIXME: all numbers will have 9 digits after 2016. 1420 * see http://portal.embratel.com.br/embratel/9-digito/ 1421 */ 1422 var phoneMask8D = new StringMask('(00) 0000-0000'), 1423 phoneMask9D = new StringMask('(00) 00000-0000'); 1424 1425 function clearValue(value) { 1426 if (!value) { 1427 return value; 1428 } 1429 1430 return value.replace(/[^0-9]/g, ''); 1431 } 1432 1433 function applyPhoneMask(value) { 1434 if (!value) { 1435 return value; 1436 } 1437 1438 var formatedValue; 1439 if (value.length < 11) { 1440 formatedValue = phoneMask8D.apply(value); 1441 } else { 1442 formatedValue = phoneMask9D.apply(value); 1443 } 1444 1445 return formatedValue.trim().replace(/[^0-9]$/, ''); 1446 } 1447 1448 return { 1449 restrict: 'A', 1450 require: '?ngModel', 1451 link: function (scope, element, attrs, ctrl) { 1452 if (!ctrl) { 1453 return; 1454 } 1455 1456 ctrl.$formatters.push(function (value) { 1457 return applyPhoneMask(validateBrPhoneNumber(ctrl, value)); 1458 }); 1459 1460 ctrl.$parsers.push(function (value) { 1461 if (!value) { 1462 return value; 1463 } 1464 1465 var cleanValue = clearValue(value); 1466 var formatedValue = applyPhoneMask(cleanValue); 1467 1468 if (ctrl.$viewValue !== formatedValue) { 1469 ctrl.$setViewValue(formatedValue); 1470 ctrl.$render(); 1471 } 1472 1473 return clearValue(formatedValue); 1474 }); 1475 1476 ctrl.$parsers.push(function (value) { 1477 return validateBrPhoneNumber(ctrl, value); 1478 }); 1479 } 1480 }; 1481 }) 1482 .directive('uiBrCepMask',function() { 1483 var cepMask = new StringMask('00000-000'); 1484 1485 function clearValue (value) { 1486 if(!value) { 1487 return value; 1488 } 1489 1490 return value.replace(/[^0-9]/g, ''); 1491 } 1492 1493 function applyCepMask (value, ctrl) { 1494 if(!value) { 1495 ctrl.$setValidity('cep', true); 1496 return value; 1497 } 1498 var processed = cepMask.process(value); 1499 ctrl.$setValidity('cep', processed.valid); 1500 var formatedValue = processed.result; 1501 return formatedValue.trim().replace(/[^0-9]$/, ''); 1502 } 1503 1504 return { 1505 restrict: 'A', 1506 require: '?ngModel', 1507 link: function(scope, element, attrs, ctrl) { 1508 if (!ctrl) { 1509 return; 1510 } 1511 1512 ctrl.$formatters.push(function(value) { 1513 return applyCepMask(value, ctrl); 1514 }); 1515 1516 ctrl.$parsers.push(function(value) { 1517 if (!value) { 1518 return applyCepMask(value, ctrl); 1519 } 1520 1521 var cleanValue = clearValue(value); 1522 var formatedValue = applyCepMask(cleanValue, ctrl); 1523 1524 if (ctrl.$viewValue !== formatedValue) { 1525 ctrl.$setViewValue(formatedValue); 1526 ctrl.$render(); 1527 } 1528 1529 return clearValue(formatedValue); 1530 }); 1531 } 1532 }; 1533 }) 1534 .directive('uiBrIeMask', ['$parse', function($parse) { 1535 1536 var ieMasks = { 1537 'AC': [{mask: new StringMask('00.000.000/000-00')}], 1538 'AL': [{mask: new StringMask('000000000')}]
1538, 1539 'AM': [{mask: new StringMask('00.000.000-0')}], 1540 'AP': [{mask: new StringMask('000000000')}], 1541 'BA': [{chars: 8, mask: new StringMask('000000-00')}, 1542 {mask: new StringMask('0000000-00')}], 1543 'CE': [{mask: new StringMask('00000000-0')}], 1544 'DF': [{mask: new StringMask('00000000000-00')}], 1545 'ES': [{mask: new StringMask('00000000-0')}], 1546 'GO': [{mask: new StringMask('00.000.000-0')}], 1547 'MA': [{mask: new StringMask('000000000')}], 1548 'MG': [{mask: new StringMask('000.000.000/0000')}], 1549 'MS': [{mask: new StringMask('000000000')}], 1550 'MT': [{mask: new StringMask('0000000000-0')}], 1551 'PA': [{mask: new StringMask('00-000000-0')}], 1552 'PB': [{mask: new StringMask('00000000-0')}], 1553 'PE': [{chars: 9, mask: new StringMask('0000000-00')}, 1554 {mask: new StringMask('00.0.000.0000000-0')}], 1555 'PI': [{mask: new StringMask('000000000')}], 1556 'PR': [{mask: new StringMask('000.00000-00')}], 1557 'RJ': [{mask: new StringMask('00.000.00-0')}], 1558 'RN': [{chars: 9, mask: new StringMask('00.000.000-0')}, 1559 {mask: new StringMask('00.0.000.000-0')}], 1560 'RO': [{mask: new StringMask('0000000000000-0')}], 1561 'RR': [{mask: new StringMask('00000000-0')}], 1562 'RS': [{mask: new StringMask('000/0000000')}], 1563 'SC': [{mask: new StringMask('000.000.000')}], 1564 'SE': [{mask: new StringMask('00000000-0')}], 1565 'SP': [{mask: new StringMask('000.000.000.000')}, 1566 {mask: new StringMask('-00000000.0/000')}], 1567 'TO': [{mask: new StringMask('00000000000')}] 1568 }; 1569 1570 function clearValue (value) { 1571 if(!value) { 1572 return value; 1573 } 1574 return value.replace(/[^0-9]/g, ''); 1575 } 1576 1577 function getMask(uf, value) { 1578 if(!uf || !ieMasks[uf]) { 1579 return undefined; 1580 } 1581 var _uf = uf.toUpperCase(); 1582 if (_uf === 'SP' && /^P/i.test(value)) { 1583 return ieMasks.SP[1].mask; 1584 } 1585 var masks = ieMasks[uf]; 1586 var i = 0; 1587 while(masks[i].chars && masks[i].chars < clearValue(value).length && i < masks.length - 1) { 1588 i++; 1589 } 1590 return masks[i].mask; 1591 } 1592 1593 function applyIEMask (value, uf, ctrl) { 1594 var mask = getMask(uf, value); 1595 if(!value || !mask) { 1596 ctrl.$setValidity('ie', true); 1597 return value; 1598 } 1599 var processed = mask.process(clearValue(value)); 1600 ctrl.$setValidity('ie', BrV.ie(uf).validate(value)); 1601 var formatedValue = processed.result; 1602 if (uf && uf.toUpperCase() === 'SP' && /^p/i.test(value)) { 1603 return 'P'+(formatedValue ? formatedValue.trim().replace(/[^0-9]$/, '') : ''); 1604 } 1605 if(!formatedValue) { 1606 return formatedValue; 1607 } 1608 return formatedValue.trim().replace(/[^0-9]$/, ''); 1609 } 1610 1611 return { 1612 restrict: 'A', 1613 require: '?ngModel', 1614 link: function(scope, element, attrs, ctrl) { 1615 var state = $parse(attrs.uiBrIeMask)(scope); 1616 1617 if (!ctrl) { 1618 return; 1619 } 1620 1621 scope.$watch(attrs.uiBrIeMask, function(newState) { 1622 state = newState; 1623 applyIEMask(ctrl.$viewValue, state, ctrl); 1624 }); 1625 1626 ctrl.$formatters.push(function(value) { 1627 return applyIEMask(value, state, ctrl); 1628 }); 1629 1630 ctrl.$parsers.push(function(value) { 1631 if (!value) { 1632 return applyIEMask(value, state, ctrl); 1633 } 1634 1635 var formatedValue = applyIEMask(value, state, ctrl); 1636 1637 if (ctrl.$viewValue !== formatedValue) { 1638 ctrl.$setViewValue(formatedValue); 1639 ctrl.$render(); 1640 } 1641 1642 if (state && state.toUpperCase() === 'SP' && /^p/i.test(value)) { 1643 return 'P'+clearValue(formatedValue); 1644 } 1645 return clearValue(formatedValue); 1646 }); 1647 } 1648 }; 1649 }]) 1650 .directive('uiBrPisMask', function () { 1651 var pisMask = new StringMask('000.0000.000-0'); 1652 function clearValue(value) { 1653 if (!value) { 1654 return value; 1655 } 1656 1657 return value.replace(/[^0-9]/g, ''); 1658 } 1659 return { 1660 restrict: 'A', 1661 require: '?ngModel', 1662 link: function (scope, element, attrs, ctrl) { 1663 if (!ctrl) { 1664 return; 1665 } 1666 1667 ctrl.$formatters.push(function (value) { 1668 return pisMask.apply(value); 1669 }); 1670 1671 ctrl.$parsers.push(function (value) { 1672 if (!value) { 1673 return value; 1674 } 1675 1676 var cleanValue = clearValue(value); 1677 var formatedValue = pisMask.apply(cleanValue); 1678 1679 if (ctrl.$viewValue !== formatedValue) { 1680 ctrl.$setViewValue(formatedValue); 1681 ctrl.$render(); 1682 } 1683 1684 return clearValue(formatedValue); 1685 }); 1686 } 1687 }; 1688 }) 1689 .directive('uiBrCtpsMask', function () { 1690 var ctpsMask = new StringMask('0000000-00000'); 1691 function clearValue(value) { 1692 if (!value) { 1693 return value; 1694 } 1695 1696 return value.replace(/[^0-9]/g, ''); 1697 } 1698 return { 1699 restrict: 'A', 1700 require: '?ngModel', 1701 link: function (scope, element, attrs, ctrl) { 1702 if (!ctrl) { 1703 return; 1704 } 1705 1706 ctrl.$formatters.push(function (value) { 1707 return ctpsMask.apply(value); 1708 }); 1709 1710 ctrl.$parsers.push(function (value) { 1711 if (!value) { 1712 return value; 1713 } 1714 1715 var cleanValue = clearValue(value); 1716 var formatedValue = ctpsMask.apply(cleanValue); 1717 1718 if (ctrl.$viewValue !== formatedValue) { 1719 ctrl.$setViewValue(formatedValue); 1720 ctrl.$render(); 1721 } 1722 1723 return clearValue(formatedValue); 1724 }); 1725 } 1726 }; 1727 }); 1728})(); 1729 1730})(angular);
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.