tipo.js
17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
**********************************************************************************
* *
* @package URBEM CNM - Soluções em Gestão Pública *
* @copyright (c) 2013 Confederação Nacional de Municípos *
* @author Confederação Nacional de Municípios *
* *
* O URBEM CNM é um software livre; você pode redistribuí-lo e/ou modificá-lo sob *
* os termos da Licença Pública Geral GNU conforme publicada pela Fundação do *
* Software Livre (FSF - Free Software Foundation); na versão 2 da Licença. *
* *
* Este programa é distribuído na expectativa de que seja útil, porém, *
* SEM NENHUMA GARANTIA; nem mesmo a garantia implícita de COMERCIABILIDADE OU *
* ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA. Consulte a Licença Pública Geral do GNU *
* para mais detalhes. *
* *
* Você deve ter recebido uma cópia da Licença Pública Geral do GNU "LICENCA.txt" *
* com este programa; se não, escreva para a Free Software Foundation Inc., *
* no endereço 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
* *
**********************************************************************************
*/
/**
* Funções
* Data de Criação: 25/07/2005
* @author Analista: Cassiano
* @author Desenvolvedor: Cassiano
* $Id: tipo.js 60017 2014-09-25 17:43:38Z franver $
Casos de uso: uc-01.01.00
*/
function inteiro(evento)
{
var expRegular = new RegExp("[0-9]","g");
var retorno = true;
var teclaPressionada;
var caracter;
if (navigator.appName == "Netscape") {
teclaPressionada = evento.which;
} else {
teclaPressionada = evento.keyCode;
}
caracter = String.fromCharCode(teclaPressionada);
if (!validaTecla(evento.keyCode)) {
if (!evento.ctrlKey && caracter.search(expRegular)
|| evento.ctrlKey && !(caracter=='c' || caracter=='x' || caracter=='v') && caracter.search(expRegular)) {
retorno = false;
}
}
return retorno;
}
function validaInteiroKeyUp( campo ) {
RegExpNumero = new RegExp ("[^0-9]","g");
campo.value = campo.value.replace( RegExpNumero, '' );
}
/*------------------------------------------------------------------+
|VERIFICA SE O VALOR INFORMADO EH UM TIPO INTERIO |
+------------------------------------------------------------------*/
function isInt( valor ){
var expRegular = new RegExp("[^0-9]","g");
var retorno = true;
if( expRegular.test( valor ) ){
retorno = false;
}
return retorno;
}
/*------------------------------------------------------------------+
|VERIFICA SE O VALOR INFORMADO EH UM TIPO FLOAT |
+------------------------------------------------------------------*/
function isFloat( valor ){
var expRegular = new RegExp("[^0-9.,]","g");
var retorno = true;
if( expRegular.test( valor ) ){
retorno = false;
}
return retorno;
}
/*------------------------------------------------------------------+
|VERIFICA SE O VALOR INFORMADO EH UMA DATA VALIDA |
+------------------------------------------------------------------*/
function isData( valor ){
var retorno = true;
function tipoData( data ){
this.value = data;
}
if( valor.length > 0 ){
campoData = new tipoData( valor );
retorno = verificaData( campoData );
}
return retorno;
}
/*------------------------------------------------------------------+
|PERMITE A ENTRADA DE CARACTERS NUMÉRICOS E DA VIGULA |
|<input type="text" onClick="javascript: return float( this );"> |
+------------------------------------------------------------------*/
function tfloat( campo, evento ){
var expRegular = new RegExp("[0-9,\-]","g");
var retorno = true;
var teclaPressionada;
var caracter;
var strTemp;
if ( navigator.appName == "Netscape" ){
teclaPressionada = evento.which;
} else {
teclaPressionada = evento.keyCode;
}
caracter = String.fromCharCode( teclaPressionada );
if( !validaTecla( evento.keyCode ) ){
if( caracter.search(expRegular) ){//NÃO PERMITE A ENTRADA DE CARACTERES DIFERENTES DE NUMEROS E VIRGULA
retorno = false;
}else{
strTemp = campo.value + String.fromCharCode( teclaPressionada );
if( strTemp.search(',') != strTemp.lastIndexOf(',') ){//VERIFICA SE JAH EXISTE VIRGULA NO CAMPO SE HOUVER NAO PERMITE A ENTRADA DE OUTRA
retorno = false;
}
}
}
return retorno;
}
/*------------------------------------------------------------------+
|PERMITE A ENTRADA DE CARACTERS NUMÉRICOS E DA VIGULA |
|<input type="text" onClick="javascript: return float( this );"> |
+------------------------------------------------------------------*/
function tfloatPonto( campo, evento ){
var expRegular = new RegExp("[0-9.]","g");
var retorno = true;
var teclaPressionada;
var caracter;
var strTemp;
if ( navigator.appName == "Netscape" ){
teclaPressionada = evento.which;
} else {
teclaPressionada = evento.keyCode;
}
caracter = String.fromCharCode( teclaPressionada );
if( !validaTecla( evento.keyCode ) ){
if( caracter.search(expRegular) ){//NÃO PERMITE A ENTRADA DE CARACTERES DIFERENTES DE NUMEROS VIRGULA
retorno = false;
}else{
strTemp = campo.value + String.fromCharCode( teclaPressionada );
if( campo.value.search( new RegExp('[.]','g') ) != -1 )
retorno = false;
if( caracter.search( new RegExp('[0-9]','g') ) != -1 )
retorno = true;
}
}
return retorno;
}
/*------------------------------------------------------------------+
|FORMATA UM NÚMERO PARA FLOAT, FORMATANDO EM MILHARES |
|E CASAS DECIMAIS |
|<input type="text" onBlur="javascript: floatDecimal(this, 2, event );"> |
+------------------------------------------------------------------*/
function floatDecimal( campo, decimais, evento ){
var camposFloat = "";
var negativo = false;
if ( campo.value.length == 0 ) {
campo.value = "0";
}
if( campo.value.length >= 0 ){
if(campo.value.search('-') == 0){
negativo = true;
campo.value = campo.value.substring(1,campo.value.length);
}else{
negativo = false;
}
var virgPos = campo.value.search(',');
if( virgPos >= 0 ){//VERIFICA SE EXISTE VRIGULA NO CAMPO
var arcamposFloat = campo.value.split(',');//QUEBRA O CAMPO NA VIRGULA
if( arcamposFloat[1].length < decimais ){
while( arcamposFloat[1].length < decimais ){//PREENCHE COM ZEROS AS CASAS DECIMAIS QUE ESTIVEREM FALTANDO
arcamposFloat[1] += "0";
}
}else{
arcamposFloat[1] = arcamposFloat[1].substr( 0, decimais );
}
var milhar = inteiroParaMilhar( arcamposFloat[0] );
if( milhar.length == 0 ){
milhar = "0";
}
camposFloat = milhar+","+arcamposFloat[1];
} else {
var zeros = "";
while( zeros.length < decimais ){
zeros += "0";
}
camposFloat = inteiroParaMilhar( campo.value )+","+zeros;
}
if(negativo == true)
campo.value = '-'+camposFloat;
else
campo.value = camposFloat;
}
return true;
}
function validaValorMaximoPermitido( campo, decimais ){
if( campo.value.length > 0 ){
var flValorMaximo = geraValorMaximoPermitido( campo, decimais );
flValorMaximo = flValorMaximo.replace( ".", "" , "g");
flValorMaximo = flValorMaximo.replace( ",", "." , "g");
var flValorCampo = campo.value.replace( ".", "" , "g");
flValorCampo = flValorCampo.replace( ",", "." , "g");
if( parseFloat( flValorMaximo ) < parseFloat(flValorCampo) ){
return false;
}else{
return true;
}
}else{
return true;
}
}
function geraValorMaximoPermitido( campo, decimais ){
var inTamanhoMaximo = campo.size - ( decimais + 1 );
var inQtdPontos = inTamanhoMaximo % 3;
inTamanhoMaximo = inTamanhoMaximo - inQtdPontos
var inValorMaximo = "";
for( var i = 0; i < inTamanhoMaximo; i++ ){
inValorMaximo += "9";
}
var inValorMaximoDecimal = "";
for( var i = 0; i < decimais ; i++ ){
inValorMaximoDecimal += "9";
}
var flValorMaximo = inteiroParaMilhar(inValorMaximo)+","+inValorMaximoDecimal;
return flValorMaximo;
}
/*------------------------------------------------------------------+
|VERIFICA SE O VALOR INFORMADO EH UMA DATA VALIDA |
|<input type="text" onBlur="javascript: return verificaData( this );"> |
+------------------------------------------------------------------*/
function verificaData(campoData)
{
var boErro = false;
if( campoData.value.length ){
if ( campoData.value.length < 6 ){
boErro = false;
}else{
var exercicio = new Date().getFullYear();
var tamanho = campoData.value.length;
if (tamanho < 10) {
if (tamanho == 9) {
if (trim(campoData.value).substr(6,3) == exercicio.toString().substr(0,3)) {
campoData.value = campoData.value.substr(0,6)+exercicio;
} else {
campoData.value = '';
}
} else {
if (tamanho == 6) {
campoData.value = campoData.value+exercicio;
} else if ((tamanho == 7 && trim(campoData.value).substr(6,1) == '2') || (tamanho == 8 && trim(campoData.value).substr(6,2) == '20')) {
campoData.value = campoData.value.substr(0,6)+exercicio;
} else if (tamanho == 8) {
campoData.value = campoData.value.substr(0,6) + '20' + campoData.value.substr(6,2);
} else {
campoData.value = '';
}
}
}
/*if ( campoData.value.length < 10 ) {
if( campoData.value.substr(6,2) > 80 ) {
campoData.value = campoData.value.substr(0,6) + '19' + campoData.value.substr(6,2);
} else {
campoData.value = campoData.value.substr(0,6) + '20' + campoData.value.substr(6,2);
}
}*/
if(confereDataValida(campoData) == false) {
return false;
}
if ( campoData.value.substr(0,2) < 1 || campoData.value.substr(0,2) > 31 ){
boErro = true;
}
if ( campoData.value.substr(3,2) < 1 || campoData.value.substr(3,2) > 12 ){
boErro = true;
}
if ( campoData.value.substr(3,2) == 4 || campoData.value.substr(3,2) == 6 || campoData.value.substr(3,2)==9 || campoData.value.substr(3,2) == 11 ){
if ( campoData.value.substr(0,2) > 30 ){
boErro = true;
}
}
if ( campoData.value.substr(3,2) == 2 ){
var bissexto = Number(campoData.value.substr(6,4)) % 4;
if ( bissexto != 0 && campoData.value.substr(0,2) > 28 ){
boErro = true;
}
if ( bissexto == 0 && campoData.value.substr(0,2) > 29 ){
boErro = true;
}
}
if( boErro ){
//campoData.focus();
boErro = false;
}else{
boErro = true;
}
}
}else{
boErro = true;
}
return boErro;
}
/*Validar se data digitada é valida
* passa a data para o formato yyyy-mm-dd para validar
* Alem de validar se a data tem os caracteres permitidos e se é valida
* entre 1000 e 2999!
*/
function confereDataValida(campoData)
{
arDataSeparada = campoData.value.split("/");
dataValidaFormatada = arDataSeparada[2]+'-'+arDataSeparada[1]+'-'+arDataSeparada[0];
// expressão regular para validar data onde serão testado parametros como ano bissexto e mes de fevereiro
return isValidDate(dataValidaFormatada, 'YMD')
}
/*------------------------------------------------------------------+
|VERIFICA SE O VALOR INFORMADO EH UM CNPJ VALIDO |
|<input type="text" onBlur="javascript: return CNPJ( this );"> |
+------------------------------------------------------------------*/
function isCNPJ(valor) {
valor = filtraAlfaNumerico( valor.value );
var primeiro = valor.substr(1,1);
var falso = true;
var size =valor.length;
var retorno = true;
var proximo;
if( size == 0 ){
retorno = false;
}
else if( size != 14 ){
retorno = false;
}else{
size--;
for (i=2; i<size-1; ++i){
proximo = ( valor.substr(i,1) );
if ( primeiro!=proximo ) {
falso = false;
}
}
if (falso){
retorno = true;
}else{
if(moduloCNPJ(valor.substring(0,valor.length - 2)) + "" + moduloCNPJ(valor.substring(0,valor.length - 1)) !=valor.substring(valor.length - 2,valor.length)) {
retorno = false;
}
}
}
return retorno;
}
function moduloCNPJ(str) {
var soma = 0;
var ind = 2;
var pos;
var retorno;
for( pos = str.length-1; pos>-1 ; pos = pos-1 ){
soma = soma + ( parseInt(str.charAt(pos)) * ind );
ind++;
if(str.length>11) {
if(ind>9) ind=2;
}
}
resto = soma - (Math.floor(soma / 11) * 11);
if(resto < 2) {
retorno = 0;
}else{
retorno = 11 - resto;
}
return retorno;
}
/*------------------------------------------------------------------+
|VERIFICA SE O VALOR INFORMADO EH UM CNPJ VALIDO |
|<input type="text" onBlur="javascript: return CNPJ( this );"> |
+------------------------------------------------------------------*/
function isCPF(valor) {
valor = filtraAlfaNumerico( valor.value );
var primeiro = valor.substr(1,1);
var falso = true;
var size = valor.length;
var retorno = true;
if (size==0){
retorno = true;
}
else if (size!=11){
retorno = false;
}else{
size--;
for (i=2; i<size-1; ++i){
proximo = (valor.substr(i,1));
if ( primeiro != proximo ){
falso = false
}
}
if (falso){
retorno = false;
}else{
if(moduloCPF(valor.substring(0,valor.length - 2)) + "" + moduloCPF(valor.substring(0,valor.length - 1)) != valor.substring(valor.length - 2,valor.length)) {
retorno = false;
}
}
}
return retorno;
}
function moduloCPF(str) {
var soma = 0;
var ind = 2;
var resto;
var retorno;
for(pos = str.length-1; pos > -1; pos = pos-1) {
soma = soma + (parseInt(str.charAt(pos)) * ind);
ind++;
if(str.length>11) {
if(ind>9) ind=2;
}
}
resto = soma - (Math.floor(soma / 11) * 11);
if(resto < 2) {
retorno = 0
}else{
retorno = 11 - resto
}
return retorno;
}
function isValidDate(dateStr, format) {
if (format == null) { format = "YMD"; }
format = format.toUpperCase();
if (format.length != 3) { format = "YMD"; }
if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
if (format.substring(0, 1) == "Y") { // If the year is first
var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
} else if (format.substring(1, 2) == "Y") { // If the year is second
var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
} else { // The year must be third
var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
}
// If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
// Check to see if the 3 parts end up making a valid date
if (format.substring(0, 1) == "M") { var mm = parts[0]; } else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
if (format.substring(0, 1) == "D") { var dd = parts[0]; } else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
if (parseFloat(dd) != dt.getDate()) { return false; }
if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
return true;
}