Redox
Redox

Reputation: 993

jQuery, not getting custom validate messages to work

I have the following field in a form:

<input id="edit-submitted-tel" name="submitted[tel]" value="telefoonnummer (optioneel)" size="4" maxlength="15" class="form-text" type="text">

I'm using jQuery Validate to check the form, it all works fine but I cannot seem to get my error messages to be custom per field. The below gives an error due to the field name and I have no clue what it should be:

$("#form").validate({           
    messages: {
        submitted[tel] : "tel error custom msg"
    }
});

What is this supposed to be when it is an array?

Upvotes: 2

Views: 655

Answers (3)

user1068963
user1068963

Reputation:

Use two quotes

$("#form").validate({           
    messages: {
        "submitted[tel]" : "tel error custom msg"
    }
});

Upvotes: 5

Ryley
Ryley

Reputation: 21216

I believe it is simply a matter of quoting your names properly.

From the documentation:

If your form consists of fields using names that aren't legal JavaScript identifiers, you >have to quote those names when using the rules option:

$("#myform").validate({
  rules: {
    // no quoting necessary
    name: "required",
    // quoting necessary!
    "user[email]": "email",
    // dots need quoting, too!
    "user.address.street": "required"
  }
});

So in your case, it should just be a matter of this:

$("#form").validate({           
    messages: {
        "submitted[tel]" : "tel error custom msg"
    }
});

Upvotes: 3

Caio
Caio

Reputation: 1

Resolvi assim

var $params = {debug:false, messages:{}};

var $c_nome = $("#c_nome").attr("name");
$params['messages'][$c_nome] = "O campo de nome é obrigatório";

var $c_email = $("#c_email").attr("name");
$params['messages'][$c_email] = "Por favor insira um e-mail válido";

var $c_assunto = $("#c_assunto").attr("name");
$params['messages'][$c_assunto] = "O campo de assunto é obrigatório";

var $c_msg = $("#c_msg").attr("name");
$params['messages'][$c_msg] = "O campo de mensagem é obrigatório";

$("#form-contato").validate($params);

Upvotes: 0

Related Questions