javascript 如何更改默认消息“请输入具有有效 mimetype 的值”。用于 HTML 文件输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22533583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:18:56  来源:igfitidea点击:

How to change default message "Please enter a value with a valid mimetype." for HTML file input

javascriptjquerycsshtml

提问by Mohan

I just came across the acceptattribute of <input type="file">and i really like it as i see that i don't need to write seperate validation for input file type if i use this attribute. its very simple like

我刚刚遇到了accept属性,<input type="file">我真的很喜欢它,因为我看到如果我使用这个属性,我不需要为输入文件类型编写单独的验证。它非常简单,就像

<input type="file" accept="image/*">will accept only image files..

<input type="file" accept="image/*">将只接受图像文件..

<input type="file" accept="audio/*">will accept only audio files..

<input type="file" accept="audio/*">将只接受音频文件..

<input type="file" accept="video/*">will accept only video files..

<input type="file" accept="video/*">将只接受视频文件..

However i see that if i some how select a different file then i see an error message

但是我看到如果我选择不同的文件,那么我会看到一条错误消息

Please enter a value with a valid mimetype.

Please enter a value with a valid mimetype.

i just want to know if it i can somehow change this error message and print it so something of my choice.. also is it possible to add some css to this error message ??

我只是想知道我是否可以以某种方式更改此错误消息并将其打印出来以便我选择.. 还可以向此错误消息添加一些 css 吗?

采纳答案by Bud Damyanov

Try using a little bit of JavaScript magic (mark the oninvalidattribute):

尝试使用一点 JavaScript 魔法(标记oninvalid属性):

<input type="file" accept="image/*" oninvalid="setCustomValidity('Please, blah, blah, blah ')"/>

回答by Ricardo Lerma

This working using jquery validator:

这使用 jquery 验证器工作:

<input type="file" accept="image/*" data-rule-required="true" data-msg-accept="Your message" />