Last active
June 6, 2019 02:23
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
May be the RegExp need to add the ^
like [^\d]+$ will the most suitable