validate a number with javascrip

0

Note: validate letter by letter and work well, but the problem is when the user pastes the data with numbers and letters I only have to receive a string of 11 digits, but when I put a letter it passes the validations that I have, how do I solve it?

I decided to make a parse and validate that it is a number but the data is truncated and if it passes as a numeric

Here my code

function buscarCuenta()

{

//alert("buscar cuenta");
id ="num_credito";

var valor = document.getElementById(id).value;
 var valor2=getCookie("num_credito");
    var n = valor.length;
    alert(valor);
if (valor.localeCompare(valor2)!=0)
{
    if(n==11)
     {    

        var a = parseInt(valor);
        alert(a);

         if(Number.isInteger(a)==false)
                  {
                    alert("No es un numero valido Verificar");
                    //document.getElementById(id).value = "";
                  }
                    if(Number.isInteger(a)==true)
                     {
                            setCookie("num_credito", valor);
                            putLoader();


                         var url="php/core.php";      
                        var values="Opcion=buscarTasa";
                        $.ajax({   
                            type: "POST",
                            url:url,
                            data:values,
                            success: function(datos){
                                $('#listaPendientes').html(datos);
                              }
                        });

                    //buscarCuenta();
                  }



    }//fin del if

}//fin 2 if

//  alert("Credito repetido");



}
    
asked by sanlegas 11.08.2018 в 03:01
source

4 answers

0

You must use a regular expression to validate that the string only contains the characters from 0 to 9 regardless of the order in which the numbers are found.

Which you can do by changing:

var a = parseInt(valor);
if (Number.isInteger(a)==false)

By:

var expresionRegularSoloNumeros = /^[0-9]+$/g;
if (expresionRegularSoloNumeros.test(valor) === false)
    
answered by 11.08.2018 в 03:36
0

You must use e.preventDefault();

This prevents the backend code from executing when a condition is met.

window.addEventListener("load", function() { 
miformulario.codigo.addEventListener("keypress", soloNumeros, false); }); 
//Solo permite introducir numeros. function soloNumeros(e){ var key = 
window.event ? e.which : e.keyCode; if (key < 48 || key > 57) {  
.preventDefault(); } }
    
answered by 11.08.2018 в 03:24
0

I have one more summary HTML

  //solo numeros 
  $('.input-number').on('input', function () { 
          this.value = this.value.replace(/[^0-9]/g,'');
        });
//con letras y numeros 
 $('.form-control').on('input', function () { 
  this.value = this.value.replace(/[^0-9a-zA-ZñÑáéíóúÁÉÍÓÚ.(),@ _-]/g,'');
});
        
  
 <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
 <input type='text' class='form-control input-number'
     id='coordenaday_mass' maxlength='45'   required='required' autofocus>
JAVASCRIPT

  
    
answered by 11.08.2018 в 03:44
0

In Javascript there is the function isNaN() . Returns false when the string is a number, otherwise it sends a true .

isNaN(123) //false
isNaN('Hello') //true

This function should serve:

function validateNumber(number)
{
    var res = {}
    res.error = false;
    res.message = "";
    if(number.length == 11 && isNaN(number) == false)
    {
        res.message = "Sin problemas";
        return res;
    }
    else
    {
        res.message = "Largo no permitido o contiene letras";
        res.error = true;
        return res;
    }
};
    
answered by 11.08.2018 в 05:23