How can I validate that an INPUT TEXT does not accept special characters? ?!)) $ &. '! "@ &

0

I have a function:

<input class="tf w-input" id="txtMsj" name="txtMsj" maxlength="256" placeholder="Identificación" type="text">
<input class="bn naranja w-button"  onclick="goto('frm')" value="Siguiente">

goto( doc ){
var txtMsj  document.getElementById("txtMsj").value;
}

That helps me get the text on the screen but I do not know how to validate the special characters - > [?!)) $ &. '! "@ &] with regular expressions.

    
asked by Ricardo Sauceda 15.11.2017 в 03:00
source

3 answers

3

THIS IS THE SOLUTION THAT IMPLEMENTS:

<input class="tf w-input" id="txtCurp" name="txtCurp" maxlength="256" onkeypress="return check(event)" placeholder="No. de CURP" type="text">

function check(e) {
    tecla = (document.all) ? e.keyCode : e.which;

    //Tecla de retroceso para borrar, siempre la permite
    if (tecla == 8) {
        return true;
    }

    // Patron de entrada, en este caso solo acepta numeros y letras
    patron = /[A-Za-z0-9]/;
    tecla_final = String.fromCharCode(tecla);
    return patron.test(tecla_final);
}
    
answered by 15.11.2017 / 03:48
source
2

You can do it directly from HTML, thanks to the new HTML5 attributes:

  

This field only accepts lowercase letters:

<input type="text" name="user" placeholder="Usern" pattern="[a-z]{1,15}">
  

This expression accepts letters, numbers and hyphens with a range of 1 to 15 characters:

<input type="text" pattern="[A-Za-z0-9_-]{1,15}" name="user" placeholder="User">

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <input type="text" pattern="[A-Za-z0-9]{1,15}" name="" value="">
    <button type="button" name="button">click</button>
  </body>
</html>
    
answered by 15.11.2017 в 03:28
1

Quick solution

The first thing we should do is add a class to our Input  to then be able to refer to it from a function in Jquery.

<div class="input-group">
   <label class="control-label">Precio:</label>
   <input type="number" id="precio" class="soloNumeros" min="1" />
</div>

The min attribute is to indicate that 1 will be its minimum value,  this way it will not accept negative values.

The next step will be to create a script to prevent the user from  write symbols in the input.

jQuery('.soloNumeros').keypress(function (tecla) {
  if (tecla.charCode < 48 || tecla.charCode > 57) return false;
});
    
answered by 04.06.2018 в 21:59