0

folks.

I'm trying to validate a form with jQuery Validate plugin (http://jqueryvalidation.org/) with rangeWords option. My problem is that I need to use comma to separate the words in the form input fields and I need to allow special chars/latin chars like áéíóúàèìòùÀÈÌÒÙÁÉÍÓÚñÑüÜ

Here is my code - index.php

<form method="post" action="" id="submissao_trabalhos_selecao" name="submissao_trabalhos_selecao">
    <input name="palavras_chave" type="text" id="palavras_chave" placeholder="palavra 1, palavra 2, palavra 3..." value="" size="60" required />
</form>
<input type="submit" name="salvar" id="salvar" value="Salvar informações para continuar depois" form="submissao_trabalhos_selecao"  />

The javascript

$(document).ready(function(){

    //this addMethod allows all this chars listed above                         
    jQuery.validator.addMethod("caractereslatinos", function(value, element){
        return this.optional(element) || /^[0-9a-zA-ZáéíóúàèìòùÀÈÌÒÙÁÉÍÓÚñÑüÜ_\s]+$/.test(value)
    }, "Digite caracteres válidos.");

    $("#submissao_trabalhos_selecao").validate({
        rules: {
            palavras_chave: {required: true, rangeWords:[01, 05], caractereslatinos: true}
        },
        messages: {
            palavras_chave: "Por favor, verifique a quantidade de palavras-chave (de 1 a 5)."
        },
        submitHandler: function() {
            alert();
            //$(form).ajaxSubmit();
        }
    })
});

I don't know why this is not working. When I use the latin chars it's not possible to submit the form.

  • When it comes to text validation, I usually prefer to exclude characters that shouldn't be allowed rather than which should be accepted characters. If you're building an international/unicode application, there are just way too many valid characters to make this practical. – Jan Jul 04 '15 at 01:33
  • Hi, @Jan. I'm new at this. In fact I don't know how I do this. =D – Renato Pirei Jul 04 '15 at 01:37
  • If you go in that direction, the regexp would look something like `/^[^<>!"#€%&\/\(\)=]+$/` with all the special characters you want to exclude added. I usually just put the most common offenders in there. You need to escape some of them like `(` and `)` with \. regex101 can help you put it together and explains well what the different parts mean. – Jan Jul 04 '15 at 01:44

0 Answers0