Skip to content

Instantly share code, notes, and snippets.

@donpandix
Last active June 6, 2019 02:23
Show Gist options
  • Save donpandix/b4e0a370b65c4e57520d to your computer and use it in GitHub Desktop.
Save donpandix/b4e0a370b65c4e57520d to your computer and use it in GitHub Desktop.
Función que valida un número si es de tipo entero y lo formatea poniendo puntos separador de miles.
var fn = {
validaEntero : function ( value ) {
var RegExPattern = /[0-9]+$/;
return RegExPattern.test( value );
},
formateaNumero : function ( value ) {
if ( fn.validaEntero ( value ) ) {
var retorno = '';
value = value.toString().split('').reverse().join('');
var i = value.length;
while(i>0) retorno += ((i%3===0&&i!=value.length)?'.':'')+value.substring(i--,i);
return retorno;
}
return 0;
}
}
// USO DE LA FUNCIÓN
var valorAFormatear = "123456789"; // entrada válida
var retorno = fn.formateaNumero( valorAFormatear );
// retorno = 123.456.789
var valorAFormatear = "A123456789"; // entrada inválida
var retorno = fn.formateaNumero( valorAFormatear );
// retorno = 0
@JunIce
Copy link

JunIce commented Aug 22, 2016

May be the RegExp need to add the ^
like [^\d]+$ will the most suitable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment