How to change the content of "This field is required" in Jquery form validation plugin?

25,054

Solution 1

The messages object has several interesting attributes to adjust:

messages: {
    required: "This field is required.",
    remote: "Please fix this field.",
    email: "Please enter a valid email address.",
    url: "Please enter a valid URL.",
    ...
}

See the source.

These can be set as defaults via the setDefaults() method:

$.validator.setDefaults({
    messages: {
        required: "このフィールドは必須です"
    }
});

Solution 2

I tried the accepted answer and it did not work for me at all. I did more searching on Google and found this article.

Using this line of code solved my problem:

$.validator.messages.required = "Your new required message here!";

Solution 3

You can use the messages option in the validate method.

Share:
25,054
Steven
Author by

Steven

I'm a headhunter based in Hangzhou, China. I recruit software engineers, language experts, C-suite talents for companies. If you need my headhunting services, you can contact me via steven.tu[at]mipfanyi.com(Please replace "[at]" with "@"). If you want to get a job, you can also send a resume to my email.

Updated on May 22, 2020

Comments

  • Steven
    Steven almost 4 years

    How can I change the general message of "This field is required" in Jquery form validation plugin to "このフィールドは必須です"? The color of the message can be changed by using the following code:

    <style type="text/css">
    label.error {color: red;}
    
    </style>
    

    But how to change the content?

    I want to change all "This filed is required" messages.

    I want to change all "required" messages to "このフィールドは必須です".

    $(".selector").validate({
       rules: {
         name: "required",
         email: {
           required: true,
           email: true
         }
       },
       messages: {
         name: "Please specify your name",
         email: {
           required: "We need your email address to contact you",
           email: "Your email address must be in the format of [email protected]"
         }
       }
    })
    

    only changes specific message for specific rule and specific element.

    I wrote

     messages: { 
            required:"このフィールドは必須です"
    
            }
    

    but it doesn't work.

  • relipse
    relipse about 11 years
    you should reconsider changing accepted answer to answer below $.validator.messages.required = "Your new required message here!";