简体   繁体   English

更改所需的错误消息的语言

[英]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 . 我知道我可以使用required title="Message"语言required title="Message"添加一条消息,但是我想更改默认语言: 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. 我尝试设置lang属性,还发送了Content-Language标头,但它始终使用浏览器的默认语言。

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. 由于您使用HTML5进行验证,因此我假设这仅需要支持最新的浏览器。 So, you can use the JavaScript method setCustomValidity() . 因此,您可以使用JavaScript方法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. 您提供给该方法的字符串将更改其成员DOM元素的错误消息,该消息将显示内容无效的时间。

HTML: HTML:

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

JavaScript: JavaScript的:

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

jsFiddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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