javascript - 设置自定义 HTML5 必填字段验证消息

标签 javascript jquery html

必填字段自定义验证

我有一个包含多个输入字段的表单。我已经进行了 html5 验证

<input type="text" name="topicName" id="topicName" required />

当我在不填写此文本框的情况下提交表单时,它会显示默认消息,如

“请填写此字段”

有人可以帮我编辑这条消息吗?

我有一个 javascript 代码来编辑它,但它不起作用

$(document).ready(function() {
    var elements = document.getElementsByName("topicName");
    for (var i = 0; i < elements.length; i++) {
        elements[i].oninvalid = function(e) {
            e.target.setCustomValidity("");
            if (!e.target.validity.valid) {
                e.target.setCustomValidity("Please enter Room Topic Title");
            }
        };
        elements[i].oninput = function(e) {
            e.target.setCustomValidity("");
        };
    }
})


电子邮件自定义验证

我有以下 HTML 表单

<form id="myform">
    <input id="email" name="email" type="email" />
    <input type="submit" />
</form>


我想要的验证消息。

必填字段:请输入电子邮件地址
错误的电子邮件:“testing@.com”不是有效的电子邮件地址。 (此处,输入的电子邮件地址显示在文本框中)

我试过了。

function check(input) {  
    if(input.validity.typeMismatch){  
        input.setCustomValidity("'" + input.value + "' is not a Valid Email Address.");  
    }  
    else {  
        input.setCustomValidity("");  
    }                 
}  

此功能无法正常使用,您还有其他方法吗?不胜感激。

最佳答案

代码片段

由于这个答案得到了非常多的关注,这里是我想出的一个很好的可配置片段:

/**
  * @author ComFreek <https://stackoverflow.com/users/603003/comfreek>
  * @link https://stackoverflow.com/a/16069817/603003
  * @license MIT 2013-2015 ComFreek
  * @license[dual licensed] CC BY-SA 3.0 2013-2015 ComFreek
  * You MUST retain this license header!
  */
(function (exports) {
    function valOrFunction(val, ctx, args) {
        if (typeof val == "function") {
            return val.apply(ctx, args);
        } else {
            return val;
        }
    }

    function InvalidInputHelper(input, options) {
        input.setCustomValidity(valOrFunction(options.defaultText, window, [input]));

        function changeOrInput() {
            if (input.value == "") {
                input.setCustomValidity(valOrFunction(options.emptyText, window, [input]));
            } else {
                input.setCustomValidity("");
            }
        }

        function invalid() {
            if (input.value == "") {
                input.setCustomValidity(valOrFunction(options.emptyText, window, [input]));
            } else {
               input.setCustomValidity(valOrFunction(options.invalidText, window, [input]));
            }
        }

        input.addEventListener("change", changeOrInput);
        input.addEventListener("input", changeOrInput);
        input.addEventListener("invalid", invalid);
    }
    exports.InvalidInputHelper = InvalidInputHelper;
})(window);

用法

jsFiddle

<input id="email" type="email" required="required" />
InvalidInputHelper(document.getElementById("email"), {
  defaultText: "Please enter an email address!",

  emptyText: "Please enter an email address!",

  invalidText: function (input) {
    return 'The email address "' + input.value + '" is invalid!';
  }
});

更多详情

  • defaultText 最初显示
  • emptyText 当输入为空(被清除)时显示
  • invalidText 当输入被浏览器标记为无效时显示(例如,当它不是有效的电子邮件地址时)

您可以为这三个属性中的每一个分配一个字符串或一个函数。
如果您分配一个函数,它可以接受对输入元素(DOM 节点)的引用,并且它必须返回一个字符串,然后显示为错误消息。

兼容性

测试于:

  • Chrome 金丝雀 47.0.2
  • IE 11
  • Microsoft Edge(使用截至 2015 年 8 月 28 日的最新版本)
  • 火狐 40.0.3
  • 歌剧 31.0

旧答案

您可以在此处查看旧版本:https://stackoverflow.com/revisions/16069817/6

关于javascript - 设置自定义 HTML5 必填字段验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13798313/

相关文章:

javascript - 按标题属性过滤元素

javascript - 将 MongoDB/ExpressJS 查询存储到数组中并显示到 HTML 表中

javascript - 使用 Jquery 在垂直导航上执行下拉功能

javascript - 循环内的 jQuery 淡入/淡出

javascript - 显示来自所选单选按钮的自定义 HTML5 数据属性

javascript - free-jqGrid 搜索按钮在第二次单击时不起作用

php - 如何跟踪用户访问我的网站?

javascript - 如何从 HTML 中的内部选项获取隐藏输入

javascript - 我收到错误。 Angular .js :13550 TypeError: Cannot read property '$invalid' of undefined

javascript - jQuery - 从具有特定类的元素列表中获取点击项目的索引