RegExp option to not enter special characters

4

I want to create a function which does not allow updating a record if it has special characters.

This is the way I'm doing it. I use a RexExp and I enter the characters:

onItemUpdating: function(args)
{   
      var grupo_promocion;
      var c_barra;
      var pattern =  new RegExp(/^[^!@"#$%&\/()]*$/);

      c_barra=args.item.c_barra;
      grupo_promocion=args.item.grupo_promocion;

    if (args.item.grupo_promocion === null || args.item.grupo_promocion == "" ) {
        grupo_promocion="";
    }   

    if(pattern.test(document.getElementById("grupo_promocion"))){
          alert("No se permite ingresar caracteres como @ # & %")
          args.cancel = true;
    }else{
           updategpromocion(c_barra,args.item.grupo_promocion); 
          alert("Se actualizo el grupo de promoción correctamente")
    }

}

then I put the condition using the test function, but if I enter letters and numbers it also generates the alert and does not update me.

Do I need any other condition?

    
asked by Norbey Martinez 18.04.2017 в 23:16
source

1 answer

5

First, document.getElementById("grupo_promocion") refers to the element, not the contained text. To get the text, depending on what type of element it is, you should refer to . textContent ,

answered by 18.04.2017 / 23:33
source