Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

84
Visualizações
My javascript regex code does not work for mobile view

I used the following code in WPforms on Wordpress to only allow english characters, numbers and space char in my form. Everything works great until you try it from mobile (the same website and same form, Chrome on Android for example), it just does not work and allows you to fill out any character to the fields.. is there any way to prevent it? I mean to make it work for mobile and don't allow visitors to put special characters from mobile too?

function wpf_dev_char_restrict() {
?>
 
<script type="text/javascript">
     
    jQuery(function($){
        $( '.wpf-char-restrict' ).on( 'keypress', function(e){
             
            var regex = new RegExp('^[a-zA-Z0-9 ]+$');
            var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
             
            if (!regex.test(key)) {
                alert ( "Please fill out the form in English. Thank you!" ); // Put any message here
                e.preventDefault();
                return false;
            }
        });
         
        //Prevent any copy and paste features to by-pass the restrictions
        $( '.wpf-char-restrict' ).bind( 'copy paste', function (e) {
             
            var regex = new RegExp('^[a-zA-Z]+$');
            var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
             
            if (!regex.test(key)) {
                alert ( "Pasting feature has been disabled for this field" ); // Put any message here
                e.preventDefault();
                return false;
            }
        });
         
    });
 
</script>
 
<?php
}
add_action( 'wpforms_wp_footer_end', 'wpf_dev_char_restrict', 10 );

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use the input event. This event will fire whatever the input method is: keyboard, mouse drag/drop, context menu, clipboard, other device.

This event triggers when the input has already changed, so you'd need to keep track of the value as it was before the latest modification:

jQuery(function($){
    var accepted = "";
    $( '.wpf-char-restrict' ).on( 'input', function() {
        var regex = /^[a-z0-9 ]*$/i;
        if (!regex.test($(this).val())) {
            alert ( "Please fill out the form in English. Thank you!" );
            $(this).val(accepted); 
        } else {
            accepted = $(this).val();
        }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="wpf-char-restrict">

NB: You may want to allow for some punctuation in the input (like comma, point, ...etc).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda