Created
January 20, 2017 15:10
-
-
Save donpandix/3a067cf6d73e86119aa478139e6a8768 to your computer and use it in GitHub Desktop.
Formatea números con puntos
This file contains 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
// Formatea número | |
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