how can I restrict offensive words in javascript

1

I have a question: I want to restrict offensive words captured by localStorage in javascript, how can I do it? Currently my code is as follows:

 /*Funcion de Capturar, Almacenar datos y Limpiar campos*/
$(document).ready(function(){    
    $('#boton-guardar').click(function(){        
        /*Captura de datos escrito en los inputs*/        
        var nom = document.getElementById("nombretxt").value;
        var res = nom.charAt(0).toUpperCase() + nom.slice(1).toLowerCase();
        /*Guardando los datos en el LocalStorage*/
        localStorage.setItem("Nombre", res);
        /*Limpiando los campos o inputs*/
        document.getElementById("nombretxt").value = "";
    });   
});
/*Funcion Cargar y Mostrar datos*/
$(document).ready(function(){                          
        /*Obtener datos almacenados*/
        var nombre = localStorage.getItem("Nombre");
        /*Mostrar datos almacenados*/      
        document.getElementById("nombre").innerHTML = nombre;
});
<html>
    <head>            
        <title>Ejemplo LocalStorage</title>
        <meta charset=”utf-8″>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
    <h1>Input Nombre</h1>
    <input type="text" placeholder="Nombre" id="nombretxt"> <br>  <br>   
    <button id="boton-guardar">Guardar</button><br>
    <div>
        <label type="text" id="nombre"></label>
    </div>
    <script src="guardarNom.js"></script>
    <script src="mostrarNom.js"></script>
    
</body> 
</html>
    
asked by Anderson Rodríguez 25.09.2018 в 19:26
source

1 answer

3

The idea is to create a array where all the words you consider as bad are (badWord) , then create a function that checks if the word or name in your case is a bad word checkMalasPalabras () , and the click event of the save button that invokes the checkMalasPalabras () function.

I only grab the part of your code so you can guide yourself where you can insert or include what I did and from there you can add other things:

var malasPalabras = ['nalgas', 'basura', 'trasero'];

const checkMalasPalabras = (palabra) => {
  var rgx = new RegExp(malasPalabras.join("|")+"|" + "/gi");
  return (rgx.test(palabra));
}

$('#boton-guardar').click(() => {        
      
  var nombre = $("#nombretxt").val().toLowerCase();
  
  if(checkMalasPalabras(nombre) == true){
    window.alert("Mala palabra encontrada: " + nombre);
  }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<h1>Input Nombre</h1> (Malas Palabras para probar nalgas, trasero y basura)
<br> 
  <input type="text" placeholder="Nombre" id="nombretxt"> <br>  <br>   
<button id="boton-guardar">Guardar</button><br>
<div>
  <label type="text" id="nombre"></label>
</div>

I hope it's what you're looking for. Greetings

    
answered by 25.09.2018 / 20:01
source