简体   繁体   中英

Change the language of error message required

Is it possible to change the language of the error message required? I know that I can add a message in my language required title="Message" , but I want to change the languge of the default: please fill out this field . In other words: I do not want to supply a custom text. I want to force a language for the default messages.

I tried setting the lang attribute and also sent a Content-Language header but it always uses the browser's default language instead.

How can I do this? Thank you in advance.

Since you're using HTML5 for validation, I'm assuming this only needs to support the latest browsers. So, you can use the JavaScript method setCustomValidity() .

The string you supply to that method will change the error message for its member DOM element, which shows when the contents are invalid.

HTML:

<form>
    <input id="eml" type="email" required />
    <input type="submit" />
</form>

JavaScript:

document.getElementById('eml').setCustomValidity('Dude. That e-mail is whack!');

jsFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM