1(function(){ 2 this.Validate = function() 3 { 4 this.field = null; 5 this.type = null; 6 this.options = {}; 7 this.excludeds = []; 8 9 var defaults = { 10 formID: '', 11 fields: {}, 12 createMessage: true, 13 submithandler: function(form){form.submit()} 14 } 15 16 this.extendDefaults = function(source, properties) 17 { 18 var property; 19 20 for (property in properties) 21 { 22 if(property == 'excludeds') continue; 23 24 if(this.excludeds.includes(property)) continue; 25 26 if (properties.hasOwnProperty(property)) 27 { 28 source[property] = properties[property]; 29 } 30 } 31 32 return source; 33 } 34 35 this.methods = { 36 'required' : {'message': 'Obrigatório.', 'fname': required}, 37 'terms' : {'message': 'Você deve aceitar os termos.', 'fname': terms}, 38 'email' : {'message': 'Por favor entre com um email válido.', 'fname': validateEmail}, 39 'cpf' : {'message': 'CPF inválido.', 'fname': validateCPF}, 40 'cnpj' : {'message': 'CNPJ inválido.', 'fname': validateCNPJ}, 41 'cpf_cnpj' : {'message': 'CPF/CNPJ inválido.', 'fname': validateCPFCNPJ}, 42 'password' : {'message': 'A senha deve conter entre 5 e 20 caracteres.', 'fname': validatePassWord}, 43 'cep' : {'message': 'CEP inválido.', 'fname': validateZipCode}, 44 'date' : {'message': 'Formato inválido (dd/mm/aaaa).', fname:validateDate}, 45 'monthyear': {'message': 'Formato inválido (mm/aaaa).', fname:validateMonthYear}, 46 'range' : {'message': 'entre {0} e {1} digitos.', fname:rangelenth}, 47 'len' : {'message': 'O campo deve conter {0} digitos.', fname:len}, 48 'equalTo' : {'message': 'O valor deve ser igual ao campo {0}.', fname:equalTo}, 49 'phone' : {'message': '{0} em formato inválido.', fname:validatePhonenumber}, 50 'remote' : {'message': 'Obrigatório.', fname:remote}, 51 'cklist' : {'message': 'Selecione ao menos uma opção.', fname:validatecheckboxes} 52 }; 53 54 this.options = this.extendDefaults(defaults); 55 56 if (arguments[0] && typeof arguments[0] === "object") 57 { 58 if(arguments[0]['excludeds']) 59 { 60 this.excludeds = arguments[0]['excludeds']; 61 } 62 63 this.options = this.extendDefaults(defaults, arguments[0]); 64 } 65 66 this.form = document.getElementById(this.options.formID); 67 68 if (!this.form) return false; 69 70 var fields = this.options.fields; 71 72 for (field in fields) 73 { 74 if(this.excludeds.includes(field)) continue; 75 76 this.field = field; 77 this.type = fields[field]; 78 79 if (fields[field].cklist) 80 { 81 clicklist.call(this); 82 } 83 else 84 { 85 if (this.form[field].type == 'checkbox') 86 { 87 click.call(this); 88 } 89 else 90 { 91 focusin.call(this); 92 focusout.call(this); 93 } 94 } 95 } 96 97 var submit = this.options; 98 99 var t = this; 100 101 this.form.onsubmit = function() 102 { 103 this.fields = fields; 104 105 if(submitValidate.call(t)) 106 { 107 submit.submithandler(this); 108 } 109 110 return false; 111 } 112 113 this.AddFields = function(fields) 114 { 115 if(Object.keys(fields).length > 0) 116 { 117 for (field in fields) 118 { 119 this.options.fields[field] = fields[field]; 120 121 this.field = field; 122 this.type = fields[field]; 123 124 if (fields[field].cklist) 125 { 126 clicklist.call(this); 127 } 128 else 129 { 130 if (this.form[field].type == 'checkbox') 131 { 132 click.call(this); 133 } 134 else 135 { 136 focusin.call(this); 137 focusout.call(this); 138 } 139 } 140 } 141 } 142 } 143 144 this.addMethod = function(n,o) 145 { 146 this.methods[n] = o; 147 } 148 } 149 150 var required = function(el,t) 151 { 152 if (el.type == 'checkbox') 153 { 154 if (!el.checked) return false;
155 } 156 else 157 { 158 if(el.value.length < 1) return false; 159 } 160 161 return true; 162 } 163 164 var terms = function(el,t) 165 { 166 if (el.type == 'checkbox') 167 { 168 if (!el.checked) return false; 169 } 170 else 171 { 172 if(el.value.length < 1) return false; 173 } 174 175 return true; 176 } 177 178 var validateEmail = function(el,t) 179 { 180 var email = el.value; 181 182 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 183 184 return re.test(email); 185 } 186 187 var validateCPF = function(el,t) 188 { 189 var strCPF = el.value; 190 var Soma; 191 var Resto; 192 Soma = 0; 193 194 strCPF = strCPF.replace(/[^\d]+/g,'') 195 if(strCPF.length !== 11) return false; 196 if (strCPF == "00000000000" || strCPF == "11111111111" || strCPF == "22222222222" || strCPF == "33333333333" || strCPF == "44444444444" || strCPF == "55555555555" || strCPF == "66666666666" || strCPF == "77777777777" || strCPF == "88888888888" || strCPF == "99999999999") return false; 197 198 for (i=1; i<=9; i++) Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (11 - i); 199 Resto = (Soma * 10) % 11; 200 201 if ((Resto == 10) || (Resto == 11)) Resto = 0; 202 if (Resto != parseInt(strCPF.substring(9, 10)) ) return false; 203 204 Soma = 0; 205 for (i = 1; i <= 10; i++) Soma = Soma + parseInt(strCPF.substring(i-1, i)) * (12 - i); 206 Resto = (Soma * 10) % 11; 207 208 if ((Resto == 10) || (Resto == 11)) Resto = 0; 209 if (Resto != parseInt(strCPF.substring(10, 11) ) ) return false; 210 return true; 211 } 212 213 var validateCNPJ = function (el,t) 214 { 215 var strCNPJ = el.value; 216 217 var b = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; 218 if (/0{14}/.test(strCNPJ)) 219 return false; 220 if ((strCNPJ = strCNPJ.replace(/[^\d]/g, "")).length != 14) 221 return false; 222 for (var i = 0, n = 0; i < 12; n += strCNPJ[i] * b[++i]); 223 if (strCNPJ[12] != (((n %= 11) < 2) ? 0 : 11 - n)) 224 return false; 225 for (var i = 0, n = 0; i <= 12; n += strCNPJ[i] * b[i++]); 226 if (strCNPJ[13] != (((n %= 11) < 2) ? 0 : 11 - n)) 227 return false; 228 return true; 229 }; 230 231 var validateCPFCNPJ = function(el,t) 232 { 233 if(!validateCPF(el,t) && !validateCNPJ(el,t)) 234 { 235 return false; 236 } 237 238 return true; 239 } 240 241 var validatePassWord = function(el,t) 242 { 243 var pass = el.value; 244 245 if(pass.length > 4 && pass.length < 21 ) 246 { 247 return true; 248 } 249 250 return false; 251 } 252 253 var validateZipCode = function(el,t) 254 { 255 if(el.value.length !== 9 ) 256 { 257 return false; 258 } 259 260 return true; 261 } 262 263 var validateDate = function(el,t, args) 264 { 265 if (el.value.length < 1 && args.date === false) return true; 266 267 var dt = /^(((0[1-9]|[12][0-9]|3[01])([-.\/])(0[13578]|10|12)([-.\/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([-.\/])(0[469]|11)([-.\/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([-.\/])(02)([-.\/])(\d{4}))|((29)(\.|-|\/)(02)([-.\/])([02468][048]00))|((29)([-.\/])(02)([-.\/])([13579][26]00))|((29)([-.\/])(02)([-.\/])([0-9][0-9][0][48]))|((29)([-.\/])(02)([-.\/])([0-9][0-9][2468][048]))|((29)([-.\/])(02)([-.\/])([0-9][0-9][13579][26])))$/; 268 269 if(dt.test(el.value)) return true; 270 271 return false; 272 } 273 274 var validateMonthYear = function(el,t, args) 275 { 276 t.methods.monthyear.message = 'Formato inválido (mm/aaaa).'; 277 278 var v = el.value; 279 280 if (v.length < 1 && args.date === false) return true; 281 282 if(v.length < 7) return false; 283 284 var splitstring = v.split("/"); 285 286 if(splitstring.length != 2) return false; 287 288 var today = new Date(); 289 290 var year = today.getFullYear(); 291 292 var month = today.getMonth()+1; 293 294 if(parseInt(splitstring[0]) < 1 || parseInt(splitstring[0]) > 12) return false; 295 296 if((parseInt(splitstring[1]) < year) || (parseInt(splitstring[1]) == year && parseInt(splitstring[0]) < month )) 297 { 298 t.methods.monthyear.message = 'Validade expirada!'; 299 300 return false;
301 } 302 303 return true; 304 } 305 306 var rangelenth = function(el,t,args) 307 { 308 if(args.range.length < 2) return true; 309 310 if(el.value.length < args.range[0] || el.value.length > args.range[1]) 311 { 312 t.methods.range.message = 'entre '+args.range[0]+' e '+args.range[1]+' digitos'; 313 314 return false; 315 } 316 317 return true; 318 } 319 320 var len = function(el,t,args) 321 { 322 if(args.len.length < 1) return true; 323 324 l = args.len; 325 326 if(l < 1) return true; 327 328 if(el.value.length < l || el.value.length > l) 329 { 330 t.methods.len.message = 'O campo deve conter '+l+' digitos'; 331 332 return false; 333 } 334 335 return true; 336 } 337 338 var equalTo = function(el,t,args) 339 { 340 if(args.equalTo.length < 2) return true; 341 342 eqEl = args.equalTo; 343 344 if(!eqEl) return true; 345 346 var newEl = document.querySelector(eqEl[0]); 347 348 if (!newEl) return true; 349 350 if(newEl.value != el.value || newEl.value.length < 1 ) 351 { 352 t.methods.equalTo.message = 'O valor deve ser igual ao campo '+eqEl[1]+'.'; 353 354 return false; 355 } 356 357 return true; 358 } 359 360 var validatePhonenumber = function(el, t, args) 361 { 362 var th = t; 363 364 if(args.phone.length < 2) return true; 365 366 var valid = args.phone[0]; 367 368 var t = args.phone[1]; 369 370 if (el.value.length < 1 && valid === false) return true; 371 372 var numsStr = el.value.replace(/[^0-9]/g,''); 373 374 if(numsStr == "0000000000") 375 { 376 th.methods.phone.message = 'telefone em formato inválido'; 377 378 return false; 379 } 380 381 var tt = (t == 'phone' && el.value.length < 15) ? 'Telefone' : 'Celular'; 382 383 if((numsStr > 0 && numsStr[0] == '0') || (numsStr > 2 && numsStr[2] == '0')) 384 { 385 th.methods.phone.message = tt + ' em formato inválido'; 386 387 return false; 388 } 389 390 if (el.value.length == 14 && t == 'phone') return true; 391 392 if (t == 'cellphone' || el.value.length > 14) 393 { 394 var numsStr = el.value.replace(/[^0-9]/g,''); 395 396 var dig = numsStr.substr(2, 1); 397 398 if (el.value.length > 14 && dig == 9) return true; 399 } 400 401 if (el.value.length < 1) 402 { 403 th.methods.phone.message = 'Campo Obrigatório.'; 404 } 405 else 406 { 407 th.methods.phone.message = tt + ' em formato inválido'; 408 } 409 410 return false; 411 } 412 413 var validatecheckboxes = function (t, args) 414 { 415 if(!args.cklist) return 0; 416 417 var list = document.querySelector(args.cklist); 418 419 if(!list) return 0; 420 421 list.classList.remove('is-invalid'); 422 423 var error = list.querySelector('div.invalid-feedback'); 424 425 if(error) 426 { 427 list.removeChild(error); 428 429 list.classList.remove('is-invalid'); 430 } 431 432 var options = list.querySelectorAll('input[type="'+args.type+'"]'); 433 434 if (options.length < 1)return 0; 435 436 for (var i = 0; i < options.length; i++) 437 { 438 if(options[i].checked) return 0; 439 } 440 441 if(this.options.createMessage) 442 { 443 createErrorText(list.firstChild, t.methods.cklist.message); 444 } 445 446 list.className += ' is-invalid'; 447 448 return 1; 449 } 450 451 var remote = function(el, t, args) 452 { 453 var opt = args.remote; 454 455 var data = (opt.field) ? opt.field+'='+el.value : el.name+'='+el.value ; 456 457 var response = false; 458 459 var request = new XMLHttpRequest(); 460 request.open('POST', opt.url, false); 461 request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 462 request.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); 463 464 request.onload = function() 465 { 466 if (request.status >= 200 && request.status < 400) 467 { 468 t.methods.remote.message = opt.message; 469 470 if(request.responseText == 'true') 471 { 472 response = true; 473 } 474 } 475 }; 476 477 request.onerror = function(a){}; 478 479 request.send(data); 480 481 return response; 482 } 483 484 function focusin() 485 { 486 var form = this.form; 487 var field = this.field; 488 489 if (!form) return false;
490 if (!form[field]) return false; 491 492 form[field].addEventListener('focusin', function(el) 493 { 494 var error = el.target.parentNode.querySelector('div.invalid-feedback'); 495 496 if(error) 497 { 498 if (error.parentNode === el.target.parentNode) { 499 el.target.parentNode.removeChild(error); 500 } 501 } 502 503 el.target.classList.remove('is-invalid'); 504 }); 505 } 506 507 function focusout() 508 { 509 var form = this.form; 510 var field = this.field; 511 var fields = this.options.fields; 512 513 if (!form) return false; 514 if (!form[field]) return false; 515 516 var t = this; 517 518 form[field].addEventListener('focusout', function(el) 519 { 520 t.field = field; 521 522 validateFields.call(t) 523 }); 524 } 525 526 function clicklist() 527 { 528 var list = document.querySelector(this.type.cklist); 529 530 var options = list.querySelectorAll('input[type="'+this.type.type+'"]'); 531 532 if (options.length < 1) return; 533 534 var t = this; 535 536 for (var i = 0; i < options.length; i++) 537 { 538 options[i].addEventListener('click', function(e) 539 { 540 var opt = options; 541 542 var el = this; 543 544 if(el.checked) 545 { 546 var error = list.querySelector('div.invalid-feedback'); 547 548 if(error) 549 { 550 list.removeChild(error); 551 } 552 553 el.classList.remove('is-invalid'); 554 555 list.classList.remove('is-invalid'); 556 557 return; 558 } 559 else 560 { 561 for (var x = 0; x < opt.length; x++) 562 { 563 if(opt[x].checked) return; 564 } 565 566 if(this.options.createMessage) 567 { 568 createErrorText(list.firstChild, t.methods.cklist.message); 569 } 570 571 list.className += ' is-invalid'; 572 } 573 },false) 574 } 575 } 576 577 function click() 578 { 579 var form = this.form; 580 var field = this.field; 581 582 if (!form) return false; 583 if (!form[field]) return false; 584 585 var m = this.methods; 586 587 let options = this.options; 588 589 form[field].addEventListener('click', function(e) 590 { 591 var el = this; 592 593 if (el.checked) 594 { 595 var error = el.parentNode.querySelector('div.invalid-feedback'); 596 597 if(error) 598 { 599 el.parentNode.removeChild(error); 600 } 601 602 el.classList.remove('is-invalid'); 603 } 604 else 605 { 606 if(options.createMessage) 607 { 608 createErrorText(el, m.required.message); 609 } 610 611 el.className += ' is-invalid'; 612 } 613 614 },false); 615 } 616 617 function submitValidate() 618 { 619 var fields = this.options.fields; 620 var form = this.form; 621 var errors = 0; 622 623 var t = this; 624 625 for (field in fields) 626 { 627 t.field = field; 628 629 if(typeof fields[field] === 'object' && fields[field].cklist) 630 { 631 errors += validatecheckboxes(t,fields[field]); 632 } 633 else 634 { 635 if (!form[field]) continue; 636 637 errors += validateFields.call(t); 638 } 639 } 640 641 if(errors > 0) 642 { 643 var infocus = form.querySelector('.is-invalid').focus(); 644 } 645 646 return (errors > 0) ? false : true; 647 } 648 649 function validateFields() 650 { 651 var form = this.form; 652 var field = this.field; 653 var fields = this.options.fields; 654 655 if (!form) return false; 656 if (!form[field]) return false; 657 658 if(this.excludeds.includes(field)) return false; 659 660 var el = form[field]; 661 662 var error = el.parentNode.querySelector('div.invalid-feedback'); 663 664 if(error) 665 { 666 error.parentNode.removeChild(error); 667 } 668 669 el.classList.remove('is-invalid'); 670 671 el.classList.remove('is-valid'); 672 673 if( typeof fields[field] === 'object') 674 { 675 var keys = Object.keys(fields[field]); 676 677 if(typeof this.methods[keys[0]] !== 'undefined' ) 678 { 679 if(!this.methods[keys[0]].fname(el,this,fields[field])) 680 { 681 if(this.options.createMessage) 682 { 683 createErrorText(el, this.methods[keys[0]].message); 684 } 685 686 el.className += ' is-invalid'; 687 688 return 1; 689 } 690 else 691 { 692 693 el.className += ' is-valid'; 694 695 return 0; 696 } 697 } 698 } 699 700 if(form[field].getAttribute('disabled') == null) 701 { 702 if(typeof this.methods[fields[field]] !== 'undefined' ) 703 { 704 if(!this.methods[fields[field]].fname(el,this)) 705 { 706 if(this.options.createMessage) 707 { 708 createErrorText(el, this.methods[fields[field]].message); 709 } 710 711 el.className += ' is-invalid'; 712 713 return 1; 714 } 715 else 716 { 717 el.className += ' is-valid'; 718 } 719 } 720 else 721 { 722 if(!this.methods.required.fname(el,this)) 723 { 724 if(this.options.createMessage) 725 { 726 createErrorText(el, this.methods.required.message); 727 } 728 729 el.className += ' is-invalid'; 730 731 return 1; 732 } 733 else 734 { 735 el.className += ' is-valid'; 736 } 737 738 } 739 } 740 741 return 0; 742 } 743}());
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.