How can I validate with jquery if a field only has letters [closed]

0

How can I validate a field that only has letters but with jquery? Thanks for your help.

    
asked by Familia Valencia Hdz 03.01.2018 в 01:29
source

1 answer

5

It is done with regular expressions, it accepts letters and spaces:

function accionarLaCosaEsta(texto){
  document.getElementById("demo").innerHTML = '¿Son letras y espacios solamente? ' +  sonLetrasSolamente(texto);
}


function sonLetrasSolamente(texto){
  var regex = /^[a-zA-Z ]+$/;
  return regex.test(texto);
}  
<input type="text" name="texto" onkeypress="accionarLaCosaEsta(this.value)"/>
<div id="demo"></div>

Here I add an example with JQuery

jQuery('#texto').on('keypress', function() {
  jQuery('#demo').html('¿Son letras y espacios solamente? ' + sonLetrasSolamente(this.value));
});

function sonLetrasSolamente(texto) {
  var regex = /^[a-zA-Z ]+$/;
  return regex.test(texto);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
Con JQuery:<br />
<input type="text" id="texto" />
<div id="demo"></div>
    
answered by 03.01.2018 / 05:44
source