Skip to content

Instantly share code, notes, and snippets.

@abnersajr
Last active August 29, 2015 14:01
Show Gist options
  • Save abnersajr/cdc47cbab4d43cfa8236 to your computer and use it in GitHub Desktop.
Save abnersajr/cdc47cbab4d43cfa8236 to your computer and use it in GitHub Desktop.
Validade Object with functions used to validate values on Sitionet Agency
var Validate = {
email: function( valor ) {
regex = new RegExp( /^([a-z0-9_\.\+-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/ );
return regex.test( valor );
},
string: function ( valor ) {
return (typeof valor === "string");
},
float: function( valor ) {
valor = replaceAll( ',', '.', valor );
regex = new RegExp( /^[-+]?[0-9]+\.[0-9]+$/ );
return regex.test( valor );
},
array_int: function( valor ) {
if ( !Array.isArray ) {
Array.isArray = function( arg ) {
return Object.prototype.toString.call( arg ) === '[object Array]';
};
}
return Array.isArray( valor );
},
integer: function() {
return ( ( parseFloat( value ) == parseInt( value ) ) && !isNaN( value ) );
},
password: function( valor ) {
return ( valor.length > 3 );
},
url: function( valor ) {
regex = new RegExp( /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/ );
return regex.test( valor );
},
sexo: function( valor ) {
return ( valor == 'm' || valor == 'f' );
},
slug: function( valor ) {
regex = new RegExp( '^[a-z0-9][-a-z0-9]*$' );
return regex.test( valor );
},
data_br: function( valor ) {
regex = new RegExp( '^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$' );
return regex.test( valor );
},
hour: function( valor ) {
regex = new RegExp( '^([01]?[0-9]|2[0-3]):[0-5][0-9]$' );
return regex.test( valor );
},
cep: function( valor ) {
regex = new RegExp( '^[0-9]{5}-[0-9]{3}$' );
return regex.test( valor );
},
phone: function( valor ) {
regex = new RegExp( '^\\([0-9]{2}\\) 9?[0-9]{4}-[0-9]{4}$' );
return regex.test( valor );
},
cnpj: function( valor ) {
var i;
var c = valor.substr( 0, 12 );
var dv = valor.substr( 12, 2 );
var d1 = 0;
for ( i = 0; i < 12; i++ ) {
d1 += c.charAt( 11 - i ) * ( 2 + ( i % 8 ) );
}
if ( d1 === 0 ) return false;
d1 = 11 - ( d1 % 11 );
if ( d1 > 9 ) d1 = 0;
if ( dv.charAt( 0 ) != d1 ) {
return false;
}
d1 *= 2;
for ( i = 0; i < 12; i++ ) {
d1 += c.charAt( 11 - i ) * ( 2 + ( ( i + 1 ) % 8 ) );
}
d1 = 11 - ( d1 % 11 );
if ( d1 > 9 ) d1 = 0;
if ( dv.charAt( 1 ) != d1 ) {
return false;
}
return true;
},
cpf: function( valor ) {
var cpf = valor;
var soma;
var resto;
var i;
if ( ( cpf.length != 11 ) ||
( cpf == "00000000000" ) || ( cpf == "11111111111" ) ||
( cpf == "22222222222" ) || ( cpf == "33333333333" ) ||
( cpf == "44444444444" ) || ( cpf == "55555555555" ) ||
( cpf == "66666666666" ) || ( cpf == "77777777777" ) ||
( cpf == "88888888888" ) || ( cpf == "99999999999" ) ) {
return false;
}
soma = 0;
for ( i = 1; i <= 9; i++ ) {
soma += Math.floor( cpf.charAt( i - 1 ) ) * ( 11 - i );
}
resto = 11 - ( soma - ( Math.floor( soma / 11 ) * 11 ) );
if ( ( resto == 10 ) || ( resto == 11 ) ) {
resto = 0;
}
if ( resto != Math.floor( cpf.charAt( 9 ) ) ) {
return false;
}
soma = 0;
for ( i = 1; i <= 10; i++ ) {
soma += cpf.charAt( i - 1 ) * ( 12 - i );
}
resto = 11 - ( soma - ( Math.floor( soma / 11 ) * 11 ) );
if ( ( resto == 10 ) || ( resto == 11 ) ) {
resto = 0;
}
if ( resto != Math.floor( cpf.charAt( 10 ) ) ) {
return false;
}
return true;
},
inUnneeded: function( valor ) {
needed = Object.keys( unneeded_fields );
if ( needed.indexOf( valor ) != -1 ) {
return true;
} else {
return false;
}
},
inRequired: function( valor ) {
required = Object.keys( required_fields );
if ( required.indexOf( valor ) != -1 ) {
return true;
} else {
return false;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment